PDA

View Full Version : How can i access to the form 2 elements ?



web.designer.iq
04-19-2009, 02:21 PM
Hi,
I have 2 forms like this:


<form id="form1">
//Elements
</form>

<form id="form2">
//Elements
</form>



I want to use javascript to get the form 1 elements or form 2 elements. How can i do it please?
Note: i used forms[0] with document object but is not working.

Nile
04-19-2009, 03:06 PM
Try using getElementById() like this:


<script type="text/javascript">
document.getElementById("form1");
document.getElementById("form2");
</script>

codeexploiter
04-20-2009, 04:40 AM
<form name="form1" id="form1">
<label>Name: </label><input type="text" name="name" value="Test1" />
</form>

<form name="form2" id="form2">
<label>Name: </label><input type="text" name="name" value="Test2" />
</form>

#1

alert(document.forms['form1'].elements['name'].value);

#2

alert(document.forms['form2'].elements['name'].value);

#3

alert(document.getElementById('form1').elements['name'].value);

#4

alert(document.getElementById('form2').elements['name'].value);