.html()
Published Aug 4, 2022Updated Aug 4, 2022
Contribute to Docs
The .html()
method gets or sets the contents of an HTML element.
Syntax
Getting HTML, the .html()
method is called without a parameter:
$(selector).html();
Setting HTML, the .html()
method is called with a parameter containing the new HTML:
$(selector).html(newHTML)
Example
The following jQuery code will read and reset the HTML in #p1
when #btn
is clicked:
$("#btn").click(function() {alert($("#p1").html());$("#p1").html("<b>New Text.</b>");});
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn JavaScript on Codecademy
- Career path
Full-Stack Engineer
A full-stack engineer can get a project done from start to finish, back-end to front-end.Includes 51 CoursesWith Professional CertificationBeginner Friendly150 hours - Free course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner Friendly15 hours