jQuery HTML method - .html() [Detail Guide]

Understanding of jQuery html() method with an example: In the previous chapter, we have learned how to get form fields value using jQuery val() method. Here in this chapter will understand the usage of the jQuery html() method.

The jQuery html() method is used to get the HTML content of the given element. Its also used to set the HTML content of the selected elements.

Basically, the html() method is frequently used in jquery, for example when we want to set dynamic HTML content on div tag.

In short in jQuery change div content dynamically can be done using the jquery .html() method.

Another example is when we want to add a dynamic table row to the HTML table, then using the jQuery .html() method we can achieve the same.

What is jQuery html() method?

The jQuery html() method is the built-in jquery method, which is used to set or get the innerHTML content of the selected elements.

When we use the .html() method to get the HTML content, then it returns the HTML content only for the first matched element.

For example, if we have multiple p tag or div tags whose content we try to get using the .html() method, then it returns only the first matched element content and not all other same name elements.

But when we use .html() method to set the HTML content, then it set HTML content for all matched elements. This means html() method replaces the innerHTML for all the selected element.  

Different usage of jQuery html() method

  1. jQuery Get innerHTML of Div tag

PS: If you found this content valuable and want to thank me? 👳 Buy Me a Coffee