470,863 Members | 1,152 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,863 developers. It's quick & easy.

Blank out all input values within a div - help a New Bee

Would someone mind tell me what I am doing wrong here? I want to find
all of the input elements within a div tag with id newID and blank out
the values in the inputs. Nothing seems to happen when I run this
code.

var new_copy=document.getElementById('newID');
var inputs = new_copy.getElementsByTagName('input');
for (inp in inputs) {
inp.value='';
}

Thanks,

Greg

Feb 9 '07 #1
3 1488
On Feb 9, 4:43 pm, "gregpin...@gmail.com" <gregpin...@gmail.com>
wrote:
Would someone mind tell me what I am doing wrong here? I want to find
all of the input elements within a div tag with id newID and blank out
the values in the inputs. Nothing seems to happen when I run this
code.

var new_copy=document.getElementById('newID');
var inputs = new_copy.getElementsByTagName('input');
for (inp in inputs) {
inp.value='';
}

Thanks,

Greg
Ok fixed it, turns out I should have been doing:

var new_copy=document.getElementById('newID');
var inputs = new_copy.getElementsByTagName('input');
for (var i = 0; i < inputs.length; ++i) {
inputs[i].value='';
}

for (val in vals) type of deal doesn't work. Anyone know why?

-Greg

Feb 9 '07 #2
gr********@gmail.com said the following on 2/9/2007 4:43 PM:
Would someone mind tell me what I am doing wrong here? I want to find
all of the input elements within a div tag with id newID and blank out
the values in the inputs. Nothing seems to happen when I run this
code.

var new_copy=document.getElementById('newID');
var inputs = new_copy.getElementsByTagName('input');
for (inp in inputs) {
alert(inp);

And you will see that not all inp in inputs is an actual input.
inp.value='';
}
var new_copy=document.getElementById('newID').
var inputs = new_copy.getElementsByTagName('input');

for (var i=0;i<inputs.length;i++) {
inputs[i].value='';
}

In short, don't use for-in to iterate over a collection.
--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Feb 9 '07 #3
gr********@gmail.com said the following on 2/9/2007 5:04 PM:
On Feb 9, 4:43 pm, "gregpin...@gmail.com" <gregpin...@gmail.com>
wrote:
>Would someone mind tell me what I am doing wrong here? I want to find
all of the input elements within a div tag with id newID and blank out
the values in the inputs. Nothing seems to happen when I run this
code.

var new_copy=document.getElementById('newID');
var inputs = new_copy.getElementsByTagName('input');
for (inp in inputs) {
inp.value='';
}

Thanks,

Greg

Ok fixed it, turns out I should have been doing:

var new_copy=document.getElementById('newID');
var inputs = new_copy.getElementsByTagName('input');
for (var i = 0; i < inputs.length; ++i) {
inputs[i].value='';
}

for (val in vals) type of deal doesn't work. Anyone know why?
See my other reply. for (val in vals) iterates all properties of the
collection.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Feb 9 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by dmcconkey | last post: by
1 post views Thread by Me | last post: by
2 posts views Thread by pelletier.thomas | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.