0

Hopefully this makes sense...

I have a page that loads up a dialog box. The shell of the dialog box actually resides in the HTML of the main page (display:none). In the dialog box I have a partial view loaded up via the following call.

$('.partial').load(url, function (html) { $('.partial')[0].value = html; });

This partial view has two select boxes. When the first one is changed the second should be populated via a .getJason call. That call is below.

$.getJSON(url, function (result) {
    var dropList = $("#select");
    var options = "";
    $.each(result, function () {
        options += '<option value="' + this.ID + '">' + this.Name+'</option>';
    });

    $("#select2").empty().append(options); // doesn't work located on the partial view
    $("#test2").empty().append(options); // works located on main page
});

The .getJason call is called via a function with onChange="populateSecondSelect()" since the partial view is loaded after the page is initially loaded (the .on won't work in this situation).

The problem is the part where I am trying to load the options into the second select. The jQuery .append doesn't recognize the element. I know the calls are working because when I try and load the values in the "Test2" on the main page it works.

How can I get the jQuery to recognize the element to .append on that loaded partial view?

2 Answers 2

1

You are correct that normally the on method will not work if the content is loaded at a later time. However, the on method can be attached directly to the document object which allows events to be attached to selectors even if they haven't yet been added to the DOM.

$(document).on("change", "#select", populateSecondSelect);

While this method is needed to bind events to dynamically loaded elements, simply selecting an element and appending html should work as expected. I suspect there may be a problem elsewhere in your code. Try posting a JSFiddle of a more complete example of your code.

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

1 Comment

I changed the code around to work with the $(document).on and got that to work. Thank you. But the fact still remains that the append that fills the second select isn't working. Any thoughts how to modify the function to append with a .on or something similar?
0

Okay, I have come up with a workaround. The idea is that I need to populate the second select box and since I need to bind it with the docuemnt.on call I used the mousedown on the actual select statement.

$(document).on("mousedown", "#select2", function (){
    if(options != "") {  // that options is "global"
        $(this).empty().append(options);
        options = "";
    }      
});

This totally populates it. Great, Yay! Now, the issue is that the second select choice will show when the first selection is changed. I guess this will have to do. :(

Comments

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.