Apply Button () to an element inside a variable
If there is a variable data = '<div>... <button id="remember"> ... </button> ...</div>'
, is it possible to apply the method .button();
to the button inside that variable?
I've tried the following:
$('#remember', data).button();
but that won't work. After that, I just execute $(data).dialog();
, which works.
I came up with a workaround and that to a append
variable data
in a document, call .button()
and then call .dialog()
, but the add and remove dialog divs
in the document doesn seem to be correct.
a source to share
You can do it using .find(selector)
and .end()
like this
var data = '<div>... <button id="remember"> ... </button> ...</div>';
$(data).find("#remember").button().end().dialog();
For a breakdown of how this works:
-
$(data)
- Creates a document fragment from a string -
.find("#remember")
- finds<button>
inside -
.button()
- Creates a jQuery UI button effect for this element -
.end()
- returns the previous selector, effectively$(data)
-
.dialog()
- Creates a dialog for the entire data item, as where.end()
the chain is placed.
a source to share