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.
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).
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.
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.
getElementById() in the JavaScript will return DOM object whereas $('#id') will return jQuery object.
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> 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