Jump to content

[HTML DOM] object question


Jack McKalling

Recommended Posts

How exactly can they be used?childNodes[#], childNode, parentNode, parentNodes[#] (I don't know which exists)It hasn't been explained at the tutorials :) Much need of them though :)

Edited by Dan The Prof
Link to comment
Share on other sites

<div id="testDiv"><span id="internalspan"><b>Text</b></span></div><script>var divLayer = document.getElementById('testDiv');alert(divLayer.childNodes.length); //returns 1 (the SPAN)alert(divLayer.childNodes[0].childNodes.length); //returns 1 (the <B> tag)alert(divLayer.childNodes[0].childNodes[0].innerHTML); //returns Textalert(divLayer.childNodes[0].innerHTML); //returns <b>Text</b>alert(divLayer.childNodes[0].childNodes[0].parentNode.parentNode.innerHTML); //returns <span id... ../span></script>

Link to comment
Share on other sites

You don't understand. If I had something like this:

<div id="border"><span id="selection">Hello <b>World!</b></span></div>
Now the span has both text and an element directly inside. This way,
document.getElementById("border").childNodes[0].childNodes[0].innerHTML
won't work anymore because the second childNodes[0] is not the first content, the text "Hello " is.
Link to comment
Share on other sites

What you're asking doesn't make sense. Use:document.getElementById("border").childNodes[0].innerHTML if you just wanted the Hello <b>World!</b>Or were you looking for a way to just display text with no tags? That isn't what childNodes is specifically for, you could just strip the tags out of innerHTML otherwise.Explain it a bit more clearly what you actually want.

Link to comment
Share on other sites

What I actually want is advanced explaining at the tutorial :)You might explain it too, then i'll try to make it clear:I tried to use the childNodes thing. Tried, because it would not work :)If there is an parent element with both text and embeded child elements, and the text comes before the embeded element,

the_parent_element.childNodes[0]
Would not work because it is followed by text instead of being the first content of parent_elementI hope now I am clear :(
Link to comment
Share on other sites

Could you put up an example of what you're attempting to do?Say you have something like this:

<div id="outerDiv">Text <span>in here</span> out of here</div><script type="text/javascript">	var divLayer = document.getElementById('outerDiv')	alert(outerDiv.childNodes[0].data); //Text 	alert(outerDiv.childNodes[1].innerHTML); //in here	alert(outerDiv.childNodes[2].data); //out of here</script>

Link to comment
Share on other sites

.data? :DThat is just another new thing for me...Could someone please phonecall Refsness so they add all this to the tutorial? :)My head burns with al those newbees and I am getting burned up by questions about them all :)(It is that I do like to get answered at the forums, but more like to read the answer at the tut so I and many other people can learn it advancedly :( )I can't find anything about these things at the Html DOM tut, why not?Why are childNodes[] not a W3C standard?

Edited by Dan The Prof
Link to comment
Share on other sites

Actually, I try to avoid using .getElementsByTagName() and .all while they both are not w3c standard, .all because div layers are even only supported by netscape or some.Ahwell, I don't care about them being not standard. If I have to use them, I do, else, I'll think of something more usable.

Link to comment
Share on other sites

  • 7 months later...

Here: http://www.w3schools.com/htmldom/dom_obj_document.aspLook for what is in the column of "W3C" with getElementsByTagName("tag"), it says clearly "NO"But anyway, this is an old topic, and I just saw the answer I couldn't give you then :)What was this topic all about? :)[*Edit:]Now that I have a little more experience in the item of this topic, I do knwo how they all work, this topic may be closed.

Edited by Dan The Prof
Link to comment
Share on other sites

Here: http://www.w3schools.com/htmldom/dom_obj_document.aspLook for what is in the column of "W3C" with getElementsByTagName("tag"), it says clearly "NO"
You are correct, W3Schools does say that but that is for Level 1 DOM. In 2000 W3C made this a standard in Level 2 DOM. W3schools needs an update :)http://www.w3.org/TR/DOM-Level-2-Core/core.html
What was this topic all about? :)
If you don't know then nobody will...you started the topic :)
Link to comment
Share on other sites

You are correct, W3Schools does say that but that is for Level 1 DOM. In 2000 W3C made this a standard in Level 2 DOM. W3schools needs an update :)http://www.w3.org/TR/DOM-Level-2-Core/core.htmlIf you don't know then nobody will...you started the topic :)

Yeah, it needs an update lol :) But I think that would be easier said than done, right? Doesn't matter though, no need to rush :blink:I do know now what this is about, but Ive solved it troughout my experience (sounds awfully arrogant, lol) between then and now :blink:
Link to comment
Share on other sites

I do know now what this is about, but Ive solved it troughout my experience (sounds awfully arrogant, lol) between then and now smile.gif
No it doesn't sound arrogant (to me). It is just how it works. The longer you work at this the better you get and the more you learn.If I were to compare myself 2 years ago (before I got a job as a programmer) and myself today there is a huge diffrence. The cause being practice everyday.
Link to comment
Share on other sites

You're right :) But it might have sounded as such if I explicitly didn't noticed :) *avoiding possible unneeded negative feelings*Anyway, the issue of this toipic is solved :)

Edited by Dan The Prof
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • Create New...