tags:

views:

174

answers:

4

My function clears out a dropdownlist and then re-populates it. Do I really need all this or is there a more concise way to do this? Meaning do I need to create a new document.createElement("option"); or is there a shortcut?

     for (blah blah blah) 
     {
         objNewOption = document.createElement("option");
objNewOption.value = day;
objNewOption.text = day;
birthDay.options.add(objNewOption);
     }
A: 

Is it really that long? That's not that much to type.

If you want to use a framework, like MooTools or Prototype, they have an Element class, letting you do it in one line.

var opt = new Element('option', { value: day, text: day });
seanmonstar
A: 

The biggest problem with the document.createElement technique is that it's really SLOW. Using a framework is best, but either way, I'd suggest building the options list and setting the innerHTML property on the select box.

strOptions = "";
for (blah blah blah) 
{
    strOptions += '<option value="' + day + '">' + day + '</option>'
}
birthDay.innerHTML = strOptions;

The browser is going to be able to parse the HTML a lot faster than you'll be able to build these elements by hand.

In response to the comment, this is really why using a platform library is always worth it. In YUI3, I do this:

var fillSelectbox = function(select, optionList) {
    var i, option = '';
    for (i = 0; i < optionList.length; i += 1) {
        option += '<option value="' + optionList[i].Value + '" selected="' + (optionList[i].selected ? '"selected"' : '""') + '">' + optionList[i].Text + '</option>';
    }
    select.append(option);
    select.set('selectedIndex', -1);
};

Where select is the selectNode and optionList is a JavaScript array.

foxxtrot
Except in IE (even 8), you can't properly set the innerHTML of a SELECT tag.
seanmonstar
please explain how using a framework can speed up DOM manipulation...
Christoph
The framework normalizes out bugs in the underlying browser implementation, allowing me to do the must faster innerHTML style set on Internet Explorer. Even using platform, building a text string instead of a large number of option tags is going to be faster, which is important when dealing with relatively large data sets.
foxxtrot
btw, I did a benchmark and you're right insofar that using `innerHTML` will be ~30% faster in FF; in IE, if you add enough elements, `innerHTML` will actually be a whole order of magnitude slower!
Christoph
It is not creating elements that is slow in itself, it's that when you have a large number of them, the child node list manipulation stuff balloons. In this case using innerHTML to generate them can be useful. But otherwise, it's quite undesirable, as you have to do HTML-escaping of data to avoid cross-site-scripting problems. The `fillSelectBox` function is vulnerable to such.
bobince
Also you shouldn't blindly trust your framework. A quick look at the code of YUI3's DOM Node.append suggest that to create the options it will parse the HTML into a new select element then move the options one by one into the target element. Indeed, there's not much else you can do to achieve an append; jQuery acts similarly. This node list manipulation gives results that are slower than if you'd simply done it yourself.
bobince
+2  A: 
var new_option_element = new Option(display_label, value, selected_boolean);
David Dorward
+2  A: 

Create your own shortcut...

function x() { return document.createElement("option"); }

for(blah blah blah) {
  objNewOption = x();
}
Josh Stodola