1

I have a button on a form that is dynamically spawning a select list, which is supposed to be populated with json data, i have all of the json data being pulled in correctly my only issue is hooking the selectlist's onload event to fill itself with data, i know i could use $('#itemID').fillSelectlist() to fill it but the problem is my select list ID's are dynamically generated and i don't know of a way to concat a select like ( $("#item" + itemNum + "list") )

my current code is below:

$.fn.fillSelect = function(data) {
return this.clearSelect().each(function() {
    if (this.tagName == 'SELECT') {
        var dropdownList = this;
        $.each(data, function(index, optionData) {
            var option = new Option(optionData.Text, optionData.Value);
            if ($.browser.msie) {
                dropdownList.add(option);
            }
            else {
                dropdownList.add(option, null);
            }
        });
    }
});

}

I was attempting to hook this up to the onload of a select list like so:

$.fn.FillDDL = function() {
$.getJSON("/Controller/GetFileCategories", null, function(data) {
    this.fillSelect(data);
});

<select id="file1Category" name="file1Category" class="form_input_small" onload="fillDDL"></select>

any pointers?

EDIT: my select lists are injected to the page as html stored in a jquery script with the onload="fillDDL" value but they aren't firing when they appear, if i set a script to do $('#file1Category').fillDDL in my document.ready() script it fires but errors, so I guess i am expanding my question to include why that onload isn't working and if there is another method i should be using?

EDIT2: my fields are being added to the form like so:

$('#moreFiles').click(function() {
    $("<div class='form_row'>" +
        "<div id='file" + FileCount + "row'>" +
        "<input type='button' class='form_input_small delete' value='X' onclick=\"$('#file" + FileCount + "row').empty()\" />" +
        "<label for='file" + FileCount + "File' class='form_label_small'>File</label>" +
        "<input type='file' class='form_input_small' name='file" + FileCount + "File' id='file" + FileCount + "File' />" +
        "<label for='file" + FileCount + "Category' class='form_label_small'>Category</label>" +
        "<select id='file" + FileCount + "Category' name='file" + FileCount + "Category' class='form_input_small'></select>" +
        "</div></div><br class='clear' />")
        .hide()
        .appendTo($('#fileDiv'))
        .fadeIn('fast');
    FileCount++;
});
4
  • I assume your clear select selects all select elements and clears them and returns the jQuery result set Commented Oct 14, 2009 at 18:42
  • actually it doesn't, only the current object and btw my selectlists that are dynamic aren't the only selectlists on the page Commented Oct 14, 2009 at 18:50
  • So does each select list make a separate ajax call or do they all get the data from the same one? Commented Oct 14, 2009 at 19:00
  • they pull from the same json data source Commented Oct 14, 2009 at 19:17

3 Answers 3

2
$(function(){
 $.getJSON("/Controller/GetFileCategories", null, function(data) {
  $("select").each(function(){
    var dropdownList = this;
                $(dropdownList).clearSelect();
    $.each(data, function(index, optionData) {
     var option = new Option(optionData.Text, optionData.Value);
     if ($.browser.msie) {
       dropdownList.add(option);
     }
     else {
      dropdownList.add(option, null);
     }
    });
  });
 });
});

Edit: oops. rewrote and fixed.

Also, there's a plugin for this kind of thing

Edit: You'll have to replace $("select") with a selector that selects only the lists you want.

Sign up to request clarification or add additional context in comments.

14 Comments

how could I tie this to a dynamic select list's load?
$(function(){}) is the same as $(document).ready(function(){}). So this hooks up everything in the ready handler, no need for $.fn extensions.
The idea is that when document ready fires, it makes the ajax call, and then in the callback it selects all the <select> elements on the page, clears them, and populates them with the callback data.
yes i understand that but i mean how do I call that from a selectlist in the DOM that is injected after the $(document).ready() fires?
also see my note above, i dont want to clear all of the select elements, only my dynamic ones to fill them with my JSON data
|
1

If your data looks like this:

var data = [{"Text":"file1","Value":1},{"Text":"file2","Value":2}];

then:

var str = "";
$.each(data, function(index, optionData) {
    str += "<option value=\"" + optionData.Value + "\">" + optionData.Text + "</option>";
});

then in your click handler can put

"<select>" + str + "</select>";

Might have to write window[str] instead of str so that it's global

1 Comment

yup thats exactly what i did, worked beautifully. thanks again for all the help, ive been bombarding myself with jquery/json/etc for the past couple days, had to learn it eventually
0

looks like your capitalization may be off.

$.fn.FillDDL

vs

onload="fillDDL"

you could also do this:

<script type="text/javascript">
    $().ready(function() {
        $('#file1Category').FillDDL();
    });
</script>

2 Comments

the problem with your second example is that my field names aren't static and known beforehand, they get generated on a button click
and that was a typo on my part, with correct capitalization it still doesn't work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.