Forum Moderators: open

Message Too Old, No Replies

Traverse a HTML document using DOM

         

BigBadBurrow

2:14 pm on Jan 13, 2006 (gmt 0)

10+ Year Member



Hello,

I would like to traverse a HTML document and set all textfields to "Hello World", does anyone know how I could do this using DOM?

I don't know the names of the text fields on the web page, so what I'd like to do is traverse the HTML tree checking if the current element is <input and whether type="text" if so then do value="Hello World"

I'm new to javascript/DOM so I'm not really sure how to get started. If you have any idea then it would be most welcomed!

Thanks

BBB

Fotiman

3:17 pm on Jan 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




// Make sure browser supports this DOM method
if(!document.getElementsByTagName ) return;

// Get the input nodes
var inputNodeList = document.getElementsByTagName("INPUT");

// Make sure something was found
if(!inputNodeList ) return;

// Iterate through the list
for( var i = 0; i < inputNodeList.length; i++ )
{
// Get an individual node
var inputNode = inputNodeList.item(i);

if( inputNode.type.toLowerCase() == "text" )
{
inputNode.value = "Hello World";
}
}

Hope that helps.

BigBadBurrow

3:58 pm on Jan 13, 2006 (gmt 0)

10+ Year Member



Fotiman,

That's great and does exactly what I was wanting. Thanks very much for your help!