I have a <select> with about 20 options.. I know very basic JS so i'm stuck..
I want users to be able to select multiple options and then after they have selected them, JS will gather them and, foreach option create a line of html like this
Code:
<input type="text" name="[OPTION VALUE HERE]"/><br/>
then, it wil gather these strings of html and insert them into the div updateForm
I can do the updating the div, but the gathering the selected options and creating the string I dont know how to do...
That's pretty easy. The select element has an options collection containing all the sub elements. We just need to loop through that collection and append some new data to a string which will represent the complete HTML.
Code:
var htmlString=""
var select=document.getElementById('selectElementId')
for(var i=0;i<select.options.length;i++){
if(select.options[i].selected){
htmlString+="<input type='text' name="+ select.options[i].value +"/><br/>"
}
document.getElementById('updateForm').innerHTML=htmlString
}