Adding select element through javascript doesn't work in ie
my code works fine in Safari, and FF, but in IE the drop down is empty. Its just empty. Any ideas? This is jquery-1.5. Thanks!
var sel = document.createElement("select");
sel.setAttribute("id", key)
sel.setAttribute('name', key)
for (var option in ddHash[key]){
var optElement = document.createEl开发者_Go百科ement("option")
optElement.text = ddHash[key][option]
if (// some conditional){
optElement.selected = true
}
else {
optElement.selected = false
}
sel.appendChild(optElement)
}
I believe optElement.text
should be optElement.innerText
for IE.
From the MSDN reference for the option
object:
You can create new OPTION elements dynamically with the document.createElement method, but you cannot change properties until the new element is added to a SELECT object. Or, you can create fully formed elements by using the Option object, as follows:
var opt = new Option( 'Text', 'Value', fDefaultSelected );
So it seems you just need to call appendChild
before trying to set the properties.
精彩评论