jQuery HTML attribute

The jQuery html() attribute is used for getting and setting DOM structures of HTML elements. This attribute is used to replace the contents of a page element with given html markup.

Syntax of html() attribute

$(selector).html() 

The selector is the element on which html markup to be get or set.

Example

<p id="demo"> Hello World!</p>

<script>
$(document).ready(function(){
$('p#demo').html('Welcome to this world.');
});
</script>

Output of the above code

Hello World!

In the above example, html attribute is replaced the content within the paragraph element of id 'demo'.

Example

<p id="gettxt">
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</p>

<script>
(function( $ ) { 
$('button#getContent').click(function(){ 
alert($('p#gettxt').html());
}); 
})(jQuery);
</script>

Output of the above code

In the above example, on click the 'Get Content' button, the html attribute alerts the content within the paragraph element of id 'gettxt'.



Read more articles


General Knowledge



Learn Popular Language