PDA

View Full Version : form fields name and value


vmihai
10-08-2002, 01:09 PM
Hi

I have a html form built dinamically.
So I don't know the name and the values of the form objects(textboxes).
Is possible to find out the name of the textfields and their values using Javascript ?
Any exampe greatly appreciated!

Roy Sinclair
10-08-2002, 03:12 PM
Yes, all the form elements are available via an array named elements.


<form name="fm">
<input type="text" name="foo">
<input type="text" name="bar">
</form>


document.forms.fm.foo is also available as document.forms.elements[0] and it's name is available as document.forms.elements[0].name. Yet another way to reach this same element is document.forms.fm.elements["foo"]

Alekz
10-08-2002, 03:42 PM
Hi,
You can also use document.forms[0].elements[0].type to get the type of the element if You need it...
You could enumerate all form elements like this:

var forms_desc = new Array();
for(var i=0;i<document.forms.length;i++){
for(var k=0;k<document.forms[i].elements.length;k++){
fld_name = document.forms[i].elements[k].name;
fld_val = document.forms[i].elements[k].value;
fld_type = document.forms[i].elements[k].type;
forms_desc[forms_desc.length] = new Array(fld_name,fld_val,fld_type);
}
}

This may fail for some types of form elements (not sure), but You can change it to make different things depending on the element type...

Alex

Spookster
10-08-2002, 05:48 PM
vmihai,

Did you not read the MUST READ thread (http://codingforums.com/showthread.php?s=&threadid=1510) before posting your javascript question in this forum? Obviously not. This thread will be moved to the appropiate forum soon. In the future if you see a MUST read thread posted at the top of the forum.....READ IT first. We have guidelines posted for a reason.