html() Method - Javascript jQuery Method and Property

Javascript examples for jQuery Method and Property:html

Description

The html() method sets or returns the innerHTML of the selected elements.

When returning content, it returns the content of the FIRST matched element.

When setting content, it overwrites the content of ALL matched elements.

Syntax

Parameter Require Description
contentRequired. new content for the selected elements
function(index,currentcontent) Optional. function that returns the new content for the selected elements
  • index - the index position of the element in the set
  • currentcontent - the current HTML content of the selected element

The following code shows how to change the content of all <p> elements:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        $("p").html("Hello <b>world!</b>");
    });/*from w w  w .  j  av a2  s . c  o m*/
});
</script>
</head>
<body>

<button>Change content of all p elements</button>

<p>This is a paragraph.</p>
<p>This is another paragraph.</p>

</body>
</html>

Related Tutorials