[thelist] Newbie DOM question
Christian Heilmann
codepo8 at gmail.com
Wed Aug 16 17:19:17 CDT 2006
> I'm looking to set the value="" of any input elements with an
> attribute/value pair of type="text" to "foo".
>
> Here's what I've got:
>
> var inputs = document.getElementsByTagName("input");
> var inputType = inputs[i].getAttribute("type");
>
> for (var i=0; i< inputs.length; i++) {
> if (inputType[i].nodeValue = "text") {
> inputs[i].setAttribute("value", "foo");
> }
> }
>
> With this script, the value of every input on my page is being set to
> "foo". The conditional seems to be the problem, but I can't figure out why.
var fields=document.getElementsByTagName('input');
for(var i=0;i<fields.length;i++){
if(fields[i].type==='text'){
fields[i].value='foo';
}
}
You read an attribute, not a node, therefore nodeValue doesn't apply.
You can of course also use the get and setAttribute methods:
var fields=document.getElementsByTagName('input');
for(var i=0;i<fields.length;i++){
if(fields[i].getAttribute('type')==='text'){
fields[i].setAttribute('value','foo');
}
}
HTH
Chris
--
Chris Heilmann
Book: http://www.beginningjavascript.com
Blog: http://www.wait-till-i.com
Writing: http://icant.co.uk/
More information about the thelist
mailing list