tags:

views:

7382

answers:

6

The example I see posted all of the time seems like it's suboptimal, because it involves concatenating strings, which seems so not jQuery. It usually looks like this:

$.getJSON("/Admin/GetFolderList/", function(result) {
    for (var i = 0; i < result.length; i++) {
     options += '<option value="' + result[i].ImageFolderID + '">' + result[i].Name + '</option>';
    }
});

Is there a better way?

+8  A: 
$.getJSON("/Admin/GetFolderList/", function(result) {
    var options = $("#options");
    //don't forget error handling!
    $.each(result, function(item) {
        options.append($("<option />").val(item.ImageFolderID).text(item.Name));
    });
});

What I'm doing above is creating a new <option> element and adding it to the options list (assuming options is the ID of a drop down element.

PS My javascript is a bit rusty so the syntax may not be perfect

Andreas Grech
That's pretty close, and got me in the right direction. See my answer below.
Jeff Putz
A: 

I use the selectboxes jquery plugin. It turns your example into:

$('#idofselect').ajaxAddOption('/Admin/GetFolderList/', {}, false);
Brian Yarger
+4  A: 

Sure - make options an array of strings and use .join('') rather than += every time through the loop. Slight performance bump when dealing with large numbers of options...

var options = [];
$.getJSON("/Admin/GetFolderList/", function(result) {
    for (var i = 0; i < result.length; i++) {
        options.push('<option value="',
          result[i].ImageFolderID, '">',
          result[i].Name, '</option>');
    }
    $("#theSelect").html(options.join(''));
});

Yes. I'm still working with strings the whole time. Believe it or not, that's the fastest way to build a DOM fragment... Now, if you have only a few options, it won't really matter - use the technique Dreas demonstrates if you like the style. But bear in mind, you're invoking the browser's internal HTML parser i*2 times, rather than just once, and modifying the DOM each time through the loop... with a sufficient number of options. you'll end up paying for it, especially on older browsers.

Note: As Justice points out, this will fall apart if ImageFolderID and Name are not encoded properly...

Shog9
You should encode `result[i].ImageFolderID` and `result[i].Name` as html-attribute-value and html-text respectively. I would not assume they come from the server pre-encoded, since I would assume that the server returns json, not bastardized json.
Justice
@Justice: you're right, but since Jeff's example omitted it, I did as well. Will add a note though, thanks.
Shog9
A: 

A good article about it too to add into the mix -- http://remysharp.com/2007/01/20/auto-populating-select-boxes-using-jquery-ajax/

CodeJoust
+4  A: 

Dreas was pretty close... it's actually this (not the reference to this instead of the item in the loop):

var options = $("#options");
$.each(result, function() {
    folderList.append($("<option />").val(this.ImageFolderID).text(this.Name));
});
Jeff Putz
+1  A: 

The fastest way is this:

 $.getJSON("/Admin/GetFolderList/", function(result) {
     var optionsValues = '<select>';
        $.each(result, function(item) {
      optionsValues += '<option value="' + item.ImageFolderID + '">' + item.Name + '</option>';
        });
     optionsValues += '</select>';
     var options = $('#options');
     options.replaceWith(optionsValues);
    });

According to this link is the fastest way because you wrap everything in a single element when doing any kind of DOM insertion.

Ricibald