... ...' , ...">

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.

+2


a source to share


2 answers


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();​​​​

      

You can see a quick demo here



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.
+1


a source


No, there is no "button" inside this variable, just a string of characters. You will need to add a button to the DOB (for example using append () as you already do) before you can apply the method to that element.



Edit: Apparently not! See comment below.

+1


a source







All Articles