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

G

gregpinero

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
 
G

gregpinero

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.value='';
}

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

-Greg
 
R

Randy Webb

(e-mail address removed) 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.value='';
}

In short, don't use for-in to iterate over a collection.
 
R

Randy Webb

(e-mail address removed) said the following on 2/9/2007 5:04 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) {
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.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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top