Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

NodeValue returns null

Status
Not open for further replies.

darkling235

Programmer
Oct 30, 2006
24
US
I am working on a webapp which is supposed to take the string highlighted and selected by a user, and find what text node in the DOM that string is in.
So far I'm making progress. This is my code

var selObj = window.getSelection();
alert(selObj); //print out selection

//find DOM node
for(var x = 0; x < document.body.childNodes.length; x++)
{
var sel = selObj.containsNode(document.body.childNodes[x], true)

alert(sel); //print out if node contains string

//print out whole NODE
if(sel == true)
alert("TEXT " + document.body.childNodes[x].nodeValue);
}

The code gets the right selection, it claims to find the correct Node. But everytime I try to display the contents of the node (the very last part) which I really need to have access to, I get an empty string or a null. Can anyone please tell me what the problem would be? The only way I've had any luck at displaying a node is when I have torn out just about every tag or separater.
Can someone please help?
Thanks in advance

Thanks in advance
 
Nope. Thanks for the advice but it still returns the empty string. I'm working on FireFox and I'm not sure it support innerHTML.
 
it does.

you might be capturing a blank text node. in some browsers, every blank space in between a tag is considered a node in and of itself.

next step would be to try removing all blank space (in a test page) and running this code again.

if it is indeed the problem, you can set up a loop so you don't have to actually remove all blank spaces in the actual html.



*cLFlaVA
----------------------------
[tt]"quote goes here"[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
Thank you, that actually does work. I used a page consisting off
</HEAD>
<BODY>
IAMSAMSAMIAMTHATSAMIAMTHATSAMIAM
<input type = "button" value = "click" onClick = "myfunc()"/>
</BODY>
</HTML>

And it did work. But how can I deal with text that does have spaces? Can you give me any suggestions?
Also, why does it say there are 3 nodes in BODY. I only see 2, the text itself and the input type.
Thanks
 
do a test to see what the nodes are. see:

Code:
<body>IAMSAMSAMIAMTHATSAMIAMTHATSAMIAM<input type = "button" value = "click" onClick = "myfunc()" /></body>

i'm not sure why it's important to you if you skip blank text nodes. if you're only testing if the childNode has the selected text, then it shouldn't matter, because the blank nodes will never have them...

can you explain what you're trying to do?



*cLFlaVA
----------------------------
[tt]"quote goes here"[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top