Jump to content

Alternative to getElementById


unreal

Recommended Posts

Hi all,I've searched the forums already but did not find anything that really answers this.So the way W3cschool teaches to get a value of an element shown is like such

function getmessage(){document.getElementById("to").innerHTML=xmlDoc.getElementsByTagName("to")[0].childNodes[0].nodeValue;

and then we do a

<p><span id="to"></span>

But how do I go about display 2 or more of values from different nodes of the elements with the same tag?W3cschool uses this method

// documentElement always represents the root nodevar x=doc.documentElement;document.write("Text of first child element: ");document.write(x.childNodes[0].childNodes[0].nodeValue);document.write("<br />");document.write("Text of second child element: ");document.write(x.childNodes[1].childNodes[0].nodeValue);</script>

That would work but how do i go about having to output by using <SPAN id> ? and not using document.write in Java.That would be more flexible when it comes to dealing with css as well.Do i do a var=secondnode=document.getElementById("to").innerHTML=xmlDoc.getElementsByTagName("to")[0].childNodes[0].nodeValue;actually i tried that.. din't work.Hope to see some help .. Thanks in advance

Link to comment
Share on other sites

hey why not try to write a for/while loop for the same to span across different elements var i=0;for (i=0;i<10;i++){document.write(x.childNodes.childNodes[0].nodeValue);}Also, u can check for the total childnodes and then loop in accordingly[:)]..hope this helps....

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...