Is there a jQuery selector that takes the html of its own tag and also the content?

5

Example:

   <div id="quadro" style="float:left">
       <h1>Quadro de horários</h1>
   </div>

If I use $("#quadro").html() I only get h1, but I want the html of h1 and the div itself.

I need something dynamic, which is independent of having "relatives" or defined children.

    
asked by anonymous 12.08.2015 / 15:47

2 answers

10

jQuery does not have this function, but you can use the property Element.outerHTML , as it is a property of the javascript object (not jQuery) you need to get the element with .get(0) or as array (in the example) to get the first element selected.

That way it stays:

$(".quadro")[0].outerHTML

It's still possible to do with pure javascript, using querySelector() :

document.querySelector('.quadro').outerHTML;
    
12.08.2015 / 15:53
0

If you use

$(".quadro") At this point you have all the elements that contain this class inside it, and with that you can navigate the element and its childs.

If you use the .html () method, you will have to append the html of your childs (string), what you really need, the element inside the selector, or html in string?

    
12.08.2015 / 15:56