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

Asked

Viewed 188 times

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 H1’s html and div’s own.

I need something dynamic, regardless of having "relatives" or defined children.

  • 2

    first I believe that the correct one would be like $(".quadro"). html(), because you are treating a class and not an id

  • Beauty, it was just a distraction at the time of formulating. I will correct.

2 answers

10


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

That way it stays:

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

It is still possible to do it with pure javascript, using querySelector():

document.querySelector('.quadro').outerHTML;

0

If you use

$(".quadro") at that moment you have all the elements that contain this class within it, and with that you can navigate the element and your children.

if you use the method. html(), you will contain append the html of your Childs(string), what you really need, the element within the selector, or the string html ?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.