Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: Get HTML including the selector?

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.

like image 893
Shpigford Avatar asked Apr 07 '11 21:04

Shpigford


People also ask

How do I get HTML using jQuery?

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.

What is $() in jQuery?

$() = 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.

How HTML elements are used in jQuery with example?

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.

How can get li id value in jQuery?

ready(function() { $('#list li'). click(function() { alert($(this). attr("id")); alert($(this). text()); }); });


1 Answers

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)

like image 131
Jamie Wong Avatar answered Sep 19 '22 09:09

Jamie Wong