Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Introduction to jQuery Events Event Types

Vic Mercier
Vic Mercier
3,276 Points

I don't understand the html function.Explain that function clearly.

$(this).html(//html code);

1 Answer

Gergely Szabo
Gergely Szabo
6,082 Points

It uses the same method like the Vanilla JS innerHTML method. The jQuery website is very helfull in this topic:

jQuery HTML reference

The good example is, that It returns with the HTML code what is inside the selected HTML element.

Example from the jQuery site:

If you select:

$( "div.demo-container" ).html();

And your HTML code is like this:

<div class="demo-container">
  <div class="demo-box">Demonstration Box</div>
</div>

You will get back this:

<div class="demo-box">Demonstration Box</div>

Of course the other use of this method is to replace the HTML inside your container element. Just like in the previous example:

$( "div.demo-container" ).html("<h1>Hello World!</h1>");

The result will be this:

<div class="demo-container">
  <h1>Hello World!</h1>
</div>

I hope It helps.