If I use firebug and type $("<p>").html() into the watch window, I get an empty string back. My guess is because the $("<p>") hasn't been rendered to the document. How can I get the markup for $("<p>") before it's added to the DOM?
views:
36answers:
3
+1
A:
You're asking for the outer HTML, not the inner, the HTML inside the <p> is indeed empty.
To get the HTML you'd have to wrap it in another element, like this:
jQuery("<div>").append($("<p>").clone()).html()
This would give you:
<p></p>
Nick Craver
2010-08-16 18:31:04
Why `.clone()` a `$("<p>")` you just created? ;o)
patrick dw
2010-08-16 20:24:21
@patrick - this is just an example...if used in practice, to not *move* the original element.
Nick Craver
2010-08-16 20:34:54
Nick - Yeah, I know. `$("p").clone()` to not move the original. But I don't imagine in practice there's ever a use for `.append($("<p>").clone())`. Was mostly just giving you a hard time.
patrick dw
2010-08-16 21:25:11
A:
What specifically are you trying to do (aside from get the HTML of a paragraph tag, obviously)?
Do you have some code you can possibly attach to this question?
El Guapo
2010-08-16 18:32:20
+2
A:
Try $( 'p' ).html() rather than $( '<p>' ).html()
Example at http://jsfiddle.net/tdg7U/
Test1 alerts $( '<p>' ).html()
Test2 alerts $( 'p' ).html()
hookedonwinter
2010-08-16 18:34:05