javascript - Does IE need an element to be inserted into the DOM before knowing it's properties? -



hope not duplicate question, quick search didn't give me results.
need create <select multiple="multiple"> element dynamically, using data this:

var _options = [     {         materialcombined: '123 - asd',         materialname: '123'     },{         materialcombined: '143123 - asdqw',         materialname: '143123'     } ]; 

now, following code works in browsers != ie

var select = new element('select ', {'multiple ': 'multiple ','size ': ((_options.length > 5) ? 5 : _options.length),'class ': 'af_ddl ','style ': 'width: 250px'});  (var = 0; < _options.length; ++i) {     var option = new element('option ').update(_options[i].materialcombined);     option.value = _options[i].materialname;     select.options.add(option); } 

but in ie, throws exception when trying call add() on select.options property. why that? ie need select inserted dom before can manipulate it?
note, have working

var tmp = ''; for(var = 0; < _options.length; ++i){     tmp += '<option value="'+_options[i].materialname+'">'+_options[i].materialcombined+'</option>'; }                                        var select = new element('select', {'multiple' : 'multiple', 'size' : ((_options.length > 5) ? 5 : _options.length) , 'class' : 'af_ddl' , 'style' : 'width:250px'}).update(tmp); 

but don't way of handling elements! :(

what message given exception? try alternate method of adding <option> elements collection swapping line

select.options.add(option); 

with

select.options[i] = option; 

edit: or can use appendchild():

select.appendchild(option); 

working demo: http://jsfiddle.net/mc8s5/

note ie9 in compat mode having trouble element itself, because of spaces following tag name , attribute names in code. removed these in working example above.


Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -