how to dynamically add options to an existing select in vanilla javascript -


i'd add option select dynamically using plain javascript. find involves jquery or tries create select dynamically well. closest thing find dynamically add input type select options in javascript latter , found doesn't involve jquery. although did try , use so:

dayselect = document.getelementbyid('dayselect'); dayselect.innerhtml += "<option'>hello world</option'>"; alert(dayselect.innerhtml) 

after did there no change select , alert gave me

hello world</option'> 

i apologize if simple i'm new @ javascript , web programming in general. thank assistance.

edit: tried given suggestions so.

dayselect = document.getelementbyid('dayselect'); myoption = document.createelement("option"); myoption.text = "hello world"; myoption.value = "hello world"; dayselect.appendchild(myoption); 

this has not changed select in page @ all. idea why? , did check code being run alert.

edit: idea did work, turned out wasn't displaying value in dropdown. don't know why can figure 1 out think.

this tutorial shows need do: add options html select box javascript

basically:

 dayselect = document.getelementbyid('dayselect');  dayselect.options[dayselect.options.length] = new option('text 1', 'value1'); 

Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -