Reputation: 11830
I have a select box element with id myselect
. The select box is in a form called myform
. I am sure it is in this form because printing this before where my error is happening prints the correct form:
>> console.log(document.forms[0].id);
myform
I get the error:
Uncaught TypeError: Cannot read property 'myselect' of undefined (anonymous function)
where I do this:
document.myform.myselect
Any ideas why?
Thank you :).
Upvotes: 0
Views: 2671
Reputation: 344743
Access the form as a property of document.forms
, not document
:
document.forms.myform.myselect
You can avoid falling into the traps of implied globals and properties in the future by just using document.getElementById()
:
document.getElementById("myform").myselect;
document.getElementById("myselect");
Upvotes: 2