Adding element elements using .innerHTML in IE

I have a txt variable that contains my html string that I need to set for a drop down list. The code works fine in all other browsers except IE. Below is the base code.

while loop with one more code

document.getElementById('theSelector').innerHTML = txt; 

where 'theSelector' is the identifier of my select element for my form

So basically IE is erased and does not generate my list. I will post my webpage below if you want to look at the source and everything that I do. If you want to see how the site should work, just launch it in another browser that is not.

http://1wux.com/Resume/signUp.html

+4
source share
3 answers

Based on your comment that it does not generate your list, and Jared comments that you are trying to add parameters, try something like this:

 var list = document.getElementById('theSelector'); var newOp = document.createElement("option"); newOp.text = "Txt"; newOp.value = "1"; list.options.add(newOp); 

EDIT

According to Jared, the following may offer you a performance advantage:

 list.options[list.options.length] = newOp; 
+4
source

As already mentioned, this is a bug in the entire version of IE. I would use the @AdamRackis solution, but if you have to build your HTML with a string, the only workaround would apparently be to use outerHTML and include your <select> in the string.

Demo: http://jsfiddle.net/ThinkingStiff/TWYUa/

HTML:

 <select id="select"></select> 

Script:

 var options = '<select id="select"><option>one</option><option>two</option></select>'; document.getElementById( 'select' ).outerHTML = options; 
+3
source

use jquery

$('#theSelector').html(txt);

-1
source

Source: https://habr.com/ru/post/1386992/


All Articles