Grab everything in a form by calling one javascipt function?

L

LaughOutLoud

Hi. I wonder is it possible to grab everything in a form by calling one
javascript function and turns it into array or object?

E.g.

<form id="form1" name="form1" method="post" action="">
<input name="fname" type="text" id="fname" />
<input name="lname" type="text" id="lname" />
<input name="email" type="text" id="email" />
</form>

I only know that using getElementById three times in order to grab the
value of the form and assign it in to variables or associative array
email = document.getElementById('email').value;
fname = document.getElementById('fname').value;
lname = document.getElementById('lname').value;

Is there any javascript or functions that just call the form name or
div id into javascript variable or turn it to array?

Thanks
 
R

Randy Webb

LaughOutLoud said the following on 9/28/2006 1:17 AM:
Hi. I wonder is it possible to grab everything in a form by calling one
javascript function and turns it into array or object?

Yes, it is called the Forms Collection:

document.forms['form1'].elements
E.g.

<form id="form1" name="form1" method="post" action="">
<input name="fname" type="text" id="fname" />
<input name="lname" type="text" id="lname" />
<input name="email" type="text" id="email" />
</form>

I only know that using getElementById three times in order to grab the
value of the form and assign it in to variables or associative array
email = document.getElementById('email').value;
fname = document.getElementById('fname').value;
lname = document.getElementById('lname').value;

formRef = document.forms['form1']
email = formRef.elements['email']
fname = formRef.elements['fname']
lname = formRef.elements['lname']
Is there any javascript or functions that just call the form name or
div id into javascript variable or turn it to array?

You could write a for-in loop to write your own.
 

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,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top