Skip to content Skip to sidebar Skip to footer

Accessing Html Input Fields With Javascript - Spaces In Input Name

I'm adding some javascript validation to a form. The form has fields that currently look like:

Solution 1:

You can use the bracket notation for objects as well as arrays:

theForm['Employee Full Name'].value == ""

This allows you to access attributes where the names are invalid in a . notation syntax:

foo.1//foo['1']
foo.this-is-wrong //foo['this-is-wrong']
foo.bar.baz //although this looks correct, it's wrong if you actually wanted foo['bar.baz']

Solution 2:

Contain them within square braces, using quotes.

if (theForm["Employee Full Name"]value ==""){   
   alert("Please enter a value for the \"Employee Full Name\" field.");     
   theForm.["Employee Full Name"].focus();   
   returnfalse;
}

Every JavaScript object can also be referenced in this way. For instance, all of the following methods have the same result:

window.location.hrefwindow["location"].hrefwindow.location["href"]
window["location"]['href'] //Single quotes / double quotes don't matter.

Post a Comment for "Accessing Html Input Fields With Javascript - Spaces In Input Name"