My code is currently returning null when trying to access an element in HTML. Strange thing is that it is working perfectly fine when testing in JSFiddle. Here is the copy of the jsfiddle link: http://jsfiddle.net/6f77q/115/.
This is what I have done in two separate files on my computer:
index.html
<script src="some.js"></script>
<select id="theid" name = "navyOp" onchange="test(this);">
<option selected = "selected">Select a Navy Op Area</option>
<option value = "AN01">AN01</option>
<option value = "AN02">AN02</option>
<option value = "AN03">AN03</option>
</select>
some.js
var earrings = document.getElementById('theid');
var anode=document.createElement("option");
anode.text="hello";
earrings.add(anode);
window.test = function(e){
alert(e.value);
}
I'm really confused as to why anode is returning null. Any ideas why this may be happening where it's working in JSFiddle but not locally?
Thanks!
`) and no onload callback is my preference - note that onload doesn't just wait for all elements to be parsed, it also waits for images to download, etc. If you do want to use a load-style handler, use jQuery's `$(document).ready(...)` (or vanilla JS's DOMContentLoaded).
– nnnnnn Sep 15 '16 at 05:36