View Single Post
Old 11-08-2012, 03:07 PM   #2
psyang
Powerplay Quarterback
 
Join Date: Jan 2010
Exp:
Default

It's sort of like a namespace issue. You could solve this in a couple ways.

1) Name the form, then access test2 via the form:
Code:
<form id="frm">
Inside Form: <select name="test2" id="test2" >
<option value="One" >One</option>
<option value="Two" >Two</option>
</select>
<a href="javascript:alert(frm.test2.value);">Add1</a>
</form><br/>
2) Use document.getElementById to get the element regardless of the hierarchy:
Code:
<form>
Inside Form: <select name="test2" id="test2" >
<option value="One" >One</option>
<option value="Two" >Two</option>
</select>
<a href="javascript:alert(document.getElementById('test2').value);">Add1</a>
</form><br/>
I normally always use document.getElementById, or the equivalent in whatever javascript framework I'm using ($(#) in jquery, Ext.get in extjs) to avoid this issue. The DOM is hierarchical, and I'm actually a little surprised that Firefox and Chrome flatten the hierarchy for form controls.
psyang is offline   Reply With Quote
The Following 2 Users Say Thank You to psyang For This Useful Post: