Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery '$(this)'

Why and when would you use $(this) instead of this?

like image 361
Salvatore DI DIO Avatar asked May 01 '26 19:05

Salvatore DI DIO


1 Answers

You use this when you are in the scope of a DOM object (or any other object for that matter)

For example:

$('a').click(function(){
     var href = $(this).prop('href'); //refers to to this a tags href
     var href1 = this.href; //ALSO refers to to this a tags href
})
like image 146
Naftali Avatar answered May 03 '26 07:05

Naftali