Detect and space with JavaScript
Solution 1
I found a workaround. Previously, I was using innerHTML to get the contents.
Now, I'm using targ.firstChild.nodeValue
where targ is the element whose content is needed.Then checking with str.charCodeAt(i)==32 || str.charCodeAt(i)==160
.
This works well.
Solution 2
String.fromCharCode(160)
worked for me. It stands for
character. if(str == ' ')
was not working in if condition, neither did trim(); but if(str == String.fromCharCode(160))
worked.
For checking normal space use if(str.trim() == '')
Diff.Thinkr
I like learning new stuff and IT is one place to do that very easily. I work mostly on C++ (Qt is something I really like) but also on HTML,JavaScript and the related but all for fun. Above all I love challenges...
Updated on June 19, 2022Comments
-
Diff.Thinkr over 1 year
I'm trying to read the content of a
contentEditable
div and extract the currently active word. ie. the word which was just entered or one which was modified.My initial approach was:
- get string as
innerHTML
- get cursor position using a function (now I can find the word that was modified)
- read backwards till a space is found (character by character comparison)
- extract the word from the point of space found.
But the problem is that the browser sometimes converts the spaces to
 
and sometimes doesn't (There is no problem if there is only one space). Then I decided to using a second loop to read in 5 chars ifa ;
is found and check against that. But this is seems very inefficient. So is there a better way to do this? - get string as