BB Design
BB Design

Reputation: 705

Use jQuery to load content into DIV and then manipulate select menu options

After page load, I am loading a select menu into a DIV like this:

$('div#searchform').load('_searchform.asp');

That load generates a SELECT menu which displays on the screen. Afterwards, I am trying to select certain options in that list, but the select menu doesn't exist in the DOM. I know how to use ON to assign events to dynamically-loaded content like this, but in this case there is no event. I just want the "select" to happen on page load. My Javascript looks like this:

$('option[value="185"]').attr('selected', 'selected');

But it doesn't see the menu, so that doesn't work. How can I get my script to see the dynamically-loaded content and manipulate the selected option?

Thank you!

Upvotes: 0

Views: 203

Answers (2)

Satpal
Satpal

Reputation: 133453

Use callback function of load

$('div#searchform').load('_searchform.asp', function() {
    //Peform manipulation here
    $('option[value="185"]').prop('selected', true);
});

Also, Use .prop() instead of .attr()

Upvotes: 0

tymeJV
tymeJV

Reputation: 104795

Do the work in the callback:

$('div#searchform').load('_searchform.asp', function() {
    //content loaded, manipulate here
});

Upvotes: 4

Related Questions