views:

28

answers:

1

I want do wrap error messages that appears (in #error div) and disappears from page to page in a jQuery UI dialog. Some of error messages are added dynamically by jQuery $('body').append('blablabla') or another insertion method. So this construction:

$('#error').load(function() {
   $('#error').dialog({
       draggable: false,
       modal: true
   });
});

is not work. This:

$('#error').live('load', function() {
   $('#error').dialog({
       draggable: false,
       modal: true
   });
});

is not work either. Can anybody tell me what should I do?

+1  A: 

Are you trying to show the dialogs when the page is loaded? If so, you're looking for $(document).ready(). Note that this will actually trigger the inside function when the DOM is ready to be traversed; this is usually what you want because you don't want to wait for the browser to completely load the page before executing your code.

If you actually do want to wait until the page is loaded, use $(window).load().

The load event can only be tied to content that is actually loaded by the browser, e.g. frames, scripts, images, etc:

The load event is sent to an element when it and all sub-elements have been completely loaded. This event can be sent to any element associated with a URL: images, scripts, frames, iframes, and the window object.

So, a <div> cannot be bound to a load event.

Josh Leitzel
That's very bad. My aim is to wrap an element into dialog when it appears in DOM.
Antiarchitect
Then just use `$(document).ready()` -- it will insert the dialog when the DOM is ready for traversal.
Josh Leitzel
Not work. I think this is because #error element added dynamically after the document is ready. Or I misunderstand how js works.
Antiarchitect
In that case why don't you just add the dialog when you add the div?
Josh Leitzel