Javascript Ajax jQuery Html PHP Example Quiz New MORE

jQuery html() method With Example

To get or set html element for a selected element in jQuery html() method is used. It is an inbuilt method of jQuery.

Syntax:

Return content:

$(selector).html()

Set content:

$(selector).html(content)

Set content using a function:

$(selector).html(function(index,currentcontent))

Example:

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("#set_button").click(function(){
    $("div").html("<p>I am Set in this div element.</p>");
  });
  $("#get_button").click(function(){
    var data=$("div").html();
    alert(data);
  });
});
</script>
</head>
<body>
 
<button id="set_button">Set the content</button>
<br><br>
<div id="div1" style="width:200px;height:80px;background-color:palegreen;">html() method jQuery</div><br>
 
<button id="get_button">Get the content</button>
<p>By click on the get the content button you can get the data of the div element</p>
</body>
</html>

Run it Yourself »