Forum Moderators: open
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
// 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.