Wednesday, May 12, 2010

JQuery.wrap()

This may be a trap when using JQuery.wrap().
for example, when I want to wrap a form with a new div, then I continue to use newly created as a JQuery UI dialog, then I found that the dialog is empty, the was not in it.

the following is the code:
var form = $('body').find('form');
//the new div
var div = $('&ltdiv/&gt');
form.wrap(div);

div.dialog();

I think the reason is when calling div.dialog(), the object div is still the newly created div, it has not been added to HTML(or its clonely was added to HTML), so to fix it, we can use the selector to get it again:

var form = $('body').find('form');
//the new div
var div = $('&ltdiv id="test"/&gt');
form.wrap(div);

div = $('body').find('#test');
//this time it works
div.dialog();

No comments: