Travis Heseman
Travis Heseman

Reputation: 11449

JQuery UI Dialog query dialog DOM

The following simply loads a jquery-ui dialog from an external html file.

$('#showdialog').click(function(e) {
    var div = $('<div>loading...</div>');
    div.dialog({
        modal: true,
        open: function() { div.load('anotherpage.html'); }
    });
    e.preventDefault();
});

After the DOM loads from the external html file, I'd like to interrogate it with JQuery. For example, supposing anothorpage.html had a bunch of anchors on it, I'd like to wire up click handlers for them when it loads into the dialog.

Any ideas?

Upvotes: 2

Views: 921

Answers (2)

Ken Redler
Ken Redler

Reputation: 23943

Look into using .live() or .delegate(), which will allow you to attach handlers to events on elements within the dynamically loaded content.

For example:

$(document).ready( function() {
    $('div.yourDynamicContainer a').live('click', function() {
        doSomething()
    })
})

Upvotes: 1

Oleg
Oleg

Reputation: 221997

You can define more then one parameter of jQuery.load (see http://api.jquery.com/load/) so after load is done you can do something:

div.load('anotherpage.html', function() {
  alert('Load was performed.');
});

place code which you need instead of alert.

Upvotes: 3

Related Questions