html - JavaScript keeps throwing "Uncaught TypeError" on addition to a select form -
i keep getting error "uncaught typeerror: cannot read property 'add' of null"
, happens on line select.add("hi!", 0);
. thought because of name mismatch, why list exists, did not solve problem. guidance awesome!
<script type="text/javascript"> for(i = 0; < 10; i++){ var names = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"] //window.alert(names[i]) document.write('<tr>'); document.write('<td>'); document.write('<select id=names[i]></select>'); var select = document.getelementbyid(names[i]); select.add("hi!", 0); document.write('</td>'); document.write('</tr>'); } </script>
thank you
you can't add text select, need make option element , add it. try this:
<script type="text/javascript"> for(i = 0; < 10; i++){ var names = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"] //window.alert(names[i]) document.write('<tr>'); document.write('<td>'); document.write('<select id=' + names[i] + '></select>'); var select = document.getelementbyid(names[i]); var option = document.createelement('option'); option.text = 'hi'; select.add(option, i) document.write('</td>'); document.write('</tr>'); } </script>
Comments
Post a Comment