views:

32

answers:

2

If have a variable data = '<div>... <button id="remember"> ... </button> ...</div>', is it possible to apply the .button(); method to a button inside that variable?

I've tried the following:

$('#remember', data).button();

but that doesn't work. After that i just do $(data).dialog();, which works.

I've come with a workaround and that's to append the variable data to the document, call the .button() and then call the .dialog(), but appending and removing dialog's divs on the document doesn't seems right.

+1  A: 

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:

  1. $(data) - Creates a document fragment out of your string
  2. .find("#remember") - Locates the <button> inside
  3. .button() - Creates the jQuery UI button effect on that element
  4. .end() - Returns the previous set selector, effectively $(data)
  5. .dialog() - Creates a dialog on the whole data element, since that's where .end() puts the chain.
Nick Craver
That actually did work, thanks Nick.
Ben
Nick, one quick question, just for curiosity: after you call dialog(); what's the context of button > $('#remember', [context]) < ?
Ben
@Ben - Not sure I follow, if you mean [`.context`](http://api.jquery.com/context/), it will be undefined at that point, since no context was originally passed, you could use any context that's an ancestor after you add it to the DOM with `.dialog()` (which inserts it in a wrapped dialog just before `</body>`). If you do `$(selector)` the context is always `document` because `$(selector)` is changed to `$(selector, document)` under the covers, really performing a `$(document).find(selector)`. You can see the original example in the answer updated here for testing: http://jsfiddle.net/DZ7xV/1/
Nick Craver
@Nick Craver - I was talking about the context used to select elements inside `$(data)`. I found that, after `$(data).dialog();` was called, selecting elements (on the dialog buttons functions) like this `$('#remember')` is a little buggy. Instead you should select them like this `$('#remember', $(this))`, being `$(this)` the first element of `$(data)` (the dialog div)
Ben
@Ben - That *should* only be buggy if there are multiple `#remember`, hopefully that's not the case...that'll give you all sorts of weird behavior. Also one tip on the context, [it'll take a DOM element](http://api.jquery.com/jQuery/), so you can just do `$('#remember', this)`, no need to wrap `this` in a jQuery object :)
Nick Craver
@Nick Craver - You're right, it seems that the problems occurs after I close and re-open the dialog. I've tried removing the document fragment using `$(data).remove();` but that doesn't seems to work. Firebugs tells me that the fragment is placed before the `</body>` tag. Any idea on this?
Ben
@Ben - Try `.dialog('destroy').dialog()` instead of just `.dialog()` so it removes the previous instance...though I'm not 100% sure on how the dialog reference works with a fragment, if it doesn't work I'll take a look when I return this afternoon.
Nick Craver
+1  A: 

No, there is no "button" inside that variable, just a string of characters. You will have to add the button to the DOB (e.g. by using append(), as you're already doing), before you can apply a method to that element.

Edit: Apparently not! See comment below.

GlenCrawford
This isn't correct, jQuery allows you to create a document fragment via `$(html)` without inserting into the DOM, at least not until `.dialog()` is called.
Nick Craver