Jump to content

document.formName.elementName['index'].value is not working


shane07

Recommended Posts

<form name="form">
<input name="data[name]"/>
<input name="data[address]"/>
<input type="submit" onclick="return validate()"/>
</form>
<script>
function validate(){
var name=document.form.data['name'].value;
alert(name);
}
</script>

This piece of code is not working.

It says

document.form.data is undefined.

Any hint about this problem?

<form name="form">
<input name="data[name]"/>
<input name="data[address]"/>
<input type="submit" onclick="return validate()"/>
</form>
<script>
function validate(){
var name=document.form.data['name'].value;
alert(name);
}
</script>

This piece of code is not working.

It says

document.form.data is undefined.

Any hint about this problem?

 

It's a syntax issue.  Since the inputs use array notation, you can't reference them quite the way you're attempting to.  Instead, try:

 

document.forms['form'].elements['data[name]'].value;

 

See also: http://www.javascripttoolbox.com/bestpractices/#squarebracket

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.