By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,673 Members | 1,214 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,673 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
3 Replies

P: n/a
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

P: n/a
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

P: n/a
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.