What is the easiest way to use jQuery to get the contents of the html tag including this tag itself? For example: how, using the selector $("#test") , write the following markup in the value of a variable?

 <div id="test"> <p> ั‚ะตัั‚ </p> </div> 

    1 answer 1

    Javascript

    Option 1

     var result = document.getElementById('test').outerHTML; console.log(result); 
     <div id="test"> <p> ั‚ะตัั‚ </p> </div> 

    Option 2

     var result = document.querySelector('#test').outerHTML; console.log(result); 
     <div id="test"> <p> ั‚ะตัั‚ </p> </div> 

    Jquery

     var result = $('#test')[0].outerHTML; console.log(result); 
     <div id="test"> <p> ั‚ะตัั‚ </p> </div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

    • Thank you for your reply! As usual, jQuery most elegant of all :) - Bokov Gleb