1

I have a requirement where I need to get the word that was typed in a contenteditable div.

Whenever I type a word and hit space I want to know what word was typed. The word can be typed towards the end or anywhere on between.

Matt
  • 74,352
  • 26
  • 153
  • 180
Nitish Borade
  • 367
  • 3
  • 18

3 Answers3

0

If you are using javascript then this stack overflow link is useful :

get the text content from a contenteditable div through javascript

Community
  • 1
  • 1
Priyanka.Patil
  • 1,177
  • 2
  • 15
  • 34
  • For my usecase i have an event 'wordentered'. This event gets fired when user hits space or enter. This event expects the word that was typed. So how do I get what word was typed when the user hit space or enter? – Nitish Borade Mar 15 '13 at 11:53
  • I have 2 doubts 1.Do you mean to say when you hit enter/space in the contenteditable div,they are considered part of the input and hence the event is not getting fired? 2.Is this issue any browser specific? – Priyanka.Patil Mar 15 '13 at 12:40
  • 1. No there is no issue firing the event. The event gets fired when spaces/enter is hit. My requirement is that when user hits space/enter, i want the word he has typed before hitting space/enter. How do I get the word that the user has typed. I don't want the complete textvalue of the contenteditable div, but as and when the user goes tying & hitting space/enter, i want to detect the word he has typed on every space/enter encountered. This word will be passed to the event eventually, but before this i want the word the user has typed when he hits space/enter. 2. No it is not browser specific. – Nitish Borade Mar 15 '13 at 13:02
  • 1
    So you mean to say you want to retrieve the immediately last word before hitting enter? try the following link : http://stackoverflow.com/questions/12917750/getting-the-last-entered-word-from-a-contenteditable-div – Priyanka.Patil Mar 15 '13 at 13:10
  • Yes, but this works if it is the last word is at the end. What about words that have been entered that aren't at the end? Say you're inputting in the middle of the text. – Nitish Borade Mar 15 '13 at 13:22
  • @Priyanka.Patil : What you are telling will not work for the askers requirement. Look at my answer for a change :), I hope it solves the askers problem – Abhijith Nagaraja Mar 16 '13 at 07:07
0

In GWT I believe contentEditable div refers to Textbox, Numberbox or RichTextArea. (If any others are there, let me know).

The solution looks fairly simple.

You can easily override the onBrowseEvent() for each of the above said widgets. Once the keystrokes matches either space or Enter get the value from the div and store it in current value or any other string. Maintain a previous value String and compare with it. Once you encounter the first change in the String, you got word which you were looking for.

Hope this helps

Abhijith Nagaraja
  • 3,370
  • 6
  • 27
  • 55
0

You can use on keyUp event to check for space and than use a diff algorithm to check for changes made. see google for "javascript diff" algos - you will masses ....

SamReciter
  • 1
  • 1
  • 1