Appending The Items Into The Combo Box May 29, 2023 Post a Comment I need to add the item in a combo box for a particular number of times.This is my code. for(i=0;i<3;i++) { othercompaniesli.innerHTML= ' Solution 1: var tmpStr = '<select onchange="document.location.href = this.options[this.selectedIndex].value;">'; for(i=0;i<3;i++) { tmpStr+= '<option VALUE="http://www.google.com">'+fStr1[0]+'</option> '; } tmpStr = '</select>'; othercompaniesli.innerHTML = tmpStr; Copy Solution 2: try othercompaniesli.innerHTML +=. Solution 3: Since you are using equal to =, it is re-assigning to the same element Use append() $('#othercompaniesli').append('<select onchange="document.location.href = this.options[this.selectedIndex].value;"><option VALUE="http://www.google.com">'+fStr1[0]+'</option> </select>'); Copy Note that your select and option elements are repeating, you need to change it accordingly. Solution 4: Place select tag out of loop var selectTag = '<select onchange="document.location.href = this.options[this.selectedIndex].value;">'; for(i=0;i<3;i++) { selectTag += '<option VALUE="http://www.google.com">'+fStr1[0]+'</option>'; } selectTag +="</select>" othercompaniesli.innerHTML = selectTag; Copy Solution 5: What you are doing is the inside the loop you are ending your select tag , so every element will have it own select opening and closing tag. and you are just updating your innerHTML with the newer element thats why its getting the last element. var openingTag= '<select onchange="document.location.href = this.options[this.selectedIndex].value;">'; for(i=0;i<3;i++) { openingTag+= '<option VALUE="http://www.google.com">'+fStr1[0]+'</option> '; } openingTag= '</select>'; othercompaniesli.innerHTML = openingTag; Copy Share Post a Comment for "Appending The Items Into The Combo Box"
Post a Comment for "Appending The Items Into The Combo Box"