Say I have this HTML:
<ul> <li id="example"><strong>Awesome</strong> example text</li> </ul>
I want to be able to do something like $('#example').html()
but right now doing that obviously only gets <strong>Awesome</strong> example text
.
So how can I get the HTML including the selected element?
ie. <li id="example"><strong>Awesome</strong> example text</li>
I'm using jQuery 1.4.4.
To get HTML content of an element using jQuery, use the html() method. The html() method gets the html contents of the first matched element.
$() = window. jQuery() $()/jQuery() is a selector function that selects DOM elements. Most of the time you will need to start with $() function. It is advisable to use jQuery after DOM is loaded fully.
The html() method in jQuery is used to get the contents of the first element in the set of matched elements or is used to set the HTML contents of every matched element. It returns the content of the first matched element. This function does not accept any arguments.
ready(function() { $('#list li'). click(function() { alert($(this). attr("id")); alert($(this). text()); }); });
In this specific case:
var outerHTML = $("<div />").append($('#example').clone()).html();
See this page for more details.
And the discussion here: http://api.jquery.com/html/ (this explains that this isn't in jQuery core and why some logical solutions won't work)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With