The html() method (innerHTML in JS) in Jquery returns the HTML content of an element.
Get HTML content
<!DOCTYPE html>
<html>
<head>
<title>Jquery html()</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<div id="main" style="border:2px solid black">
<h1>What is the full form of HTML?</h1>
<p>The full form of the HTML is hyper text markup language.</p>
</div>
<button style="margin:30px;">Show HTML content</button>
<script>
$(document).ready(function(){
$("button").click(function(){
var allcontent = $("#main").html();
alert(allcontent);
});
});
</script>
</body>
</html>
The text() method will return only text content from the element.
Set HTML content
You can write HTML content in html() method's parenthesis.
<!DOCTYPE html>
<html>
<head>
<title>Jquery html()</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<div id="main" style="border:2px solid black;width:400px;height:200px;"></div>
<button style="margin:30px;">Insert HTML content</button>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#main").html("<h1 style='color:red;'>What is the full form of HTML?</h1><p>The full form of the HTML is hyper text markup language.</p></div>");
});
});
</script>
</body>
</html>
Publish A Comment