PDA

View Full Version : DOM Issue...



pcbrainbuster
04-13-2007, 04:03 AM
Hello guys :),

This time I have started learning a little DOM and have gotten in a prediciment... -

<html>
<body onclick="alert(document.body.childNode.nodeValue)">
<input type="text" id="textb">
</body>
</html>

Though I could use document.getElementById(ID).innerHTML, but by one of the true scripting masters and some other people I have been told its not a good idea to do so...

Thanks :)...

jscheuer1
04-13-2007, 04:51 AM
childNode isn't anything in javascript because there could be one or thousands. The childNodes array or collection may be used but, it's overkill here.

innerHTML would be fine if the element has that property and all you wish to do is retrieve it. In the example that you have, it looks like you are going for:

document.getElementById('textb').value

pcbrainbuster
04-13-2007, 04:55 AM
You got it all wrong (guess I should have explained a little more :))...

I'm learning DOM and just want to know how I would get to the elemnt using straight forward DOM (no document.getElementById... Or InnerHTML or value (though this stuff is DOM, I already know how to use it which is why))...

tech_support
04-13-2007, 04:59 AM
Start of using valid DOCTYPE (http://alistapart.com/stories/doctype/) before using DOM
...and then have a good read at this (http://www.google.com.au/search?hl=en&client=firefox-a&rls=org.mozilla&#37;3Aen-US%3Aofficial&hs=2gD&q=Learn+DOM&btnG=Search&meta=)

jscheuer1
04-13-2007, 05:19 AM
I've got it wrong? Some things were just not meant to be done with DOM nodes, and your little example is just about as close as one can get to one of those things.

If you want to learn about DOM nodes, tech_support's advice is well taken.

pcbrainbuster
04-13-2007, 05:22 AM
Hmmmm, OK well I see...

Twey
04-13-2007, 09:17 AM
John's right, the value property is much more appropriate in this case.

You can access it as document.body.firstChild.value, but also, as tech_support said, you should be using a valid page before attempting to manipulate the DOM. There are too many possible variables in an invalid page, things are likely to break.

jscheuer1
04-13-2007, 10:48 AM
You can access it as document.body.firstChild.value

Unless I am mistaken, even using firstChild and a valid page will cause problems in some browsers like FF which will see an empty text node as the body element's firstChild. With a strict DOCTYPE, I don't think the input element could ever be valid and at the same time be the body element's firstChild.

In my experience, when seeking DOM nodes, it is almost always easier to look for the parent rather than the child.

Twey
04-13-2007, 11:08 AM
Unless I am mistaken, even using firstChild and a valid page will cause problems in some browsers like FF which will see an empty text node as the body element's firstChild. With a strict DOCTYPE, I don't think the input element could ever be valid and at the same time be the body element's firstChild.Right on both counts... I'd forgotten about the former, and the latter was the reason for the "but" in my post :)