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 trialnathanielcusano
9,808 PointsDoes $(this) refer to the $('li') object? And is $('li') called a "jquery object"?
none
2 Answers
Steven Parker
231,269 PointsLet's look at each question separately:
- Does $(this) refer to the $('li') object?
Not exactly. The $('li')
object represents all list items on the page. But when one of them is clicked on, the handler for the click event runs. Inside the handler, this represents the specific element that was clicked, and $(this)
is that same element converted into a jQuery object. But only that specific one. That's why only the one you click on vanishes.
- And is $('li') called a "jquery object"?
Yes, exactly. And as I said, it represent the collection of all list items (li
) elements on the page. So the on method attaches a handler to each one of them.
Rosario Dawson
400 PointsSteven Parker , is "this" in the given example something like e.target inside
randomDiv.addEventListener("click", (e) => { e.target.style.color = "blue"; });
Steven Parker
231,269 PointsYes, but you would not be able to use "this" in your example because arrow functions do not establish a "this" value.
But "this" would be the same as "e.target" inside a handler established with a traditional function:
randomDiv.addEventListener("click", e => { e.target.style.color = "blue"; });
// the same as:
randomDiv.addEventListener("click", function (e) { this.style.color = "blue"; });
ywang04
6,762 Pointsywang04
6,762 PointsThanks a lot. It is very clear to me.
Roudy Antenor
4,124 PointsRoudy Antenor
4,124 PointsBig thank you for that explanation!