Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

full HTML of object returned by jQuery selector

Tags:

given this html:

<li id="the_list_item"><img src="some_img"></li> 

and this selectior:

$("#the_list_item") 

I want to get the full html from the object return by the jQuery selector.

Using:

$("#the_list_item").html() 

...just gives me the inner html (the <img src="some_img"> part)

But since:

$("#the_list_item").attr("id") 

gives me 'the_list_item', this indicated that the whole list item is indeed included in the object returned.. so how do I get the full code from that object?

I want to get a String: <li id="the_list_item"><img src="some_img"></li> from my object, but can't find the way to do it.

like image 281
mikkelbreum Avatar asked Aug 20 '10 22:08

mikkelbreum


People also ask

What does a jQuery selector return?

The jQuery Object: The Wrapped Set: Selectors return a jQuery object known as the "wrapped set," which is an array-like structure that contains all the selected DOM elements. You can iterate over the wrapped set like an array or access individual elements via the indexer ($(sel)[0] for example).

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 does $() mean in jQuery?

In jQuery, the $ sign is just an alias to jQuery() , then an alias for a function. This page reports: Basic syntax is: $(selector).action() A dollar sign to define jQuery.

Which method returns the element as a jQuery object?

getElementById() in the JavaScript will return DOM object whereas $('#id') will return jQuery object.


1 Answers

I'm not sure if this works, but it might be worth a shot:

var html = $('#the_list_item')[0].outerHTML; alert(html); 

var html = $('#the_list_item')[0].outerHTML;  console.log(html);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>  <ul>    <li id="the_list_item"><img src="some_img"></li>  </ul>
like image 89
Tatu Ulmanen Avatar answered Oct 03 '22 23:10

Tatu Ulmanen