Spell check using JavaScript

Can anyone let me know how to implement spell check using javascript code? I am using a text area control in a web page (just like a rich text editor in hotmail/yahoo) and i want to use javascript to implement spell check on that text area when a space is pressed (as done in word) that will compare the custome dictionary maintained in a simple variable and will change the color of that particular word whose spellings are not matched?

i hope someone will be able to help me out.

Thanx in advance


  • That'll be hard, ranging to impossible.

    There's no way (that I know of) to change the color of a particular word (or to underline/change the look of in any way, shape or form), although you can select it, at least in Mozilla/Firefox ([textarea].setSelection(x,y) IIRC). I don't know about IE, but given the nature of its selection operancy, if it is possible, it's probably not worth it - especially since it would be annoying to have to constatly deselect text if you were typing in a foreign language or something, for example. I suppose you could have multiple options of what to do when it finds an error, though, and selection wouldn't be so bad then.

    You also wouldn't be able to store the dictionary in a 'simple variable'. A variable, certainly, but by no means a simple one. There's no way to read files, so you'd have to keep it all in the script itself, and that would be big if it would be at all decent-sized. You could have it in an included .js file or something (or even in the HTML, but you wouldn't be able to make the containing element display:none because the content effectively doesn't exist), though. I'd reccomend 26 different variables, one of each letter, to make matching easier.

    As for the actual checking, it would be far less hassle to just have a spellcheck button, and then you could do it server-side - cleaner, and you could use an external dctionary. If you want to do it as-you-type though, assign a function to the onKeyPress event that checks which key was pressed - you can do that by passing the variable event as a parameter, and checking said parameter's keyCode property - a space will return 46 or 47, I think. If the kay was a space, get the content of the textarea, grab the last word, remove any non-letters from it via regEx, and check to see if it's in the dictionary - you'll have to check which dictionary it would go in if there's more than one. If it doesn't occur, do something like add it to a list of mis-spelt words.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


In this Discussion