I have a <select>
element on my page which has onchange
propery set:
<select id="MySelect" onchange="return myOnChange(this);">Whatever</select>
The event handler goes like this:
function myOnChange(select) {
var parentNode = select.parentNode;
//whatever
}
Once the page has loaded the user changes the selection inside the dropdown and the event handler is invoked and parentNode
is bound to something (whatever it is, it's not undefined and that's enough).
However if I add a document.ready()
:
$(document).ready(function () {
var select = $('#MySelect');
var parentNode = select.parentNode;
//whatever
});
or window.load()
:
$(window).load(function () {
var select = $('#MySelect');
var parentNode = select.parentNode;
//whatever
});
then once either of them is invoked then parentNode
is undefined.
Why is this? How do I have my handler invoked once the page has loaded such that parentNode
is not undefined?