How to get the outer html of an element using jQuery ? Last Updated : 15 Feb, 2023 Comments Improve Suggest changes Like Article Like Report Sometimes, there is a need to get the entire HTML element by its id and not merely its contents, for doing so, we shall use the HTML DOM outerHTML Property to get the outer HTML of HTML element. Syntax: document.getElementById("your-element-id").outerHTML) You can use a variable and initialize it to the above to get the value of the outer HTML element. Below is an example that illustrates how to fetch the outer HTML element of an HTML element and store it in a variable newVar. Example 1: In this example, it has a div that encloses a paragraph with id = "demo". When the on-screen button is pressed the JavaScript pushes an alert message on the browser window with the outer HTML of the HTML element with the given id. HTML <div id="demo"> <p>This is the text inside</p> </div> <button onclick="myFunction()">Try it</button> <p id="gfg"></p> <script> function myFunction() { var newVar = document.getElementById("demo") .outerHTML; document.getElementById("gfg").innerHTML = newVar; } </script> Output: How to get the outer html of an element using jQuery ? Example 2: This example illustrates that the outerHTML DOM displays only the HTML element whose id is given and not its parent HTML <div> <p id="demo">This is the text inside</p> </div> <p id="gfg"></p> <button onclick="myFunction()">Try it</button> <script> function myFunction() { var newVar = document.getElementById("demo") .outerHTML; document.getElementById("gfg").innerHTML = newVar; } </script> Output: How to get the outer html of an element using jQuery ? Comment More infoAdvertise with us Next Article How to get the outer html of an element using jQuery ? S suchetaggarwal4 Follow Improve Article Tags : Web Technologies JQuery How To jQuery-Questions Similar Reads How to get title of current HTML page using jQuery ? Suppose you have given an HTML page and the task is to get the title of an HTML page with the help of only jQuery. There are two approaches that are discussed below: Approach 1: The $('title') jQuery selector is used to select the title element of the document and we can use text() method on the sel 2 min read How to attach a method to HTML element event using jQuery ? In this article, we are going to learn, how can we attach a method to an HTML element event using jQuery.There are various events like click(), mouseenter(), mouseout(), etc which are available in jQuery. For attaching a method to an HTML elements event, we will need a jQuery method on(), which will 2 min read How to get text contents of all matched elements using jQuery ? In this article, we will see how to get the text content of all matched class elements using jQuery. To get the text content, we use the text() method, which helps to set or return the text content of the element. Syntax: $('Selector').text();Approach 1: We create a div element that contains multipl 2 min read How to get the text without HTML element using JavaScript ? Given an HTML document containing some elements and the task is to get the text inside an HTML element using JavaScript. There are two methods to get the text without HTML element which are listed below: Using innerText propertyUsing textContent property Using innerText property: We can use innerTex 1 min read How to Get the Content of an HTML Comment using JavaScript ? In this article, we will learn how to get the content of an HTML Comment Using Javascript. Comments are a best practice in programming and software development. In general, they can explain why a coding decision was made or what needs to be done to improve the code you're working on. HTML tags (incl 3 min read Like