What is "this" exactly?


#1



https://www.codecademy.com/en/courses/web-beginner-en-4hxyb/0/1?content_from=make-an-interactive-website%3Ajquery-events


first someone please tell me which HTML class two "this"s refer then please explain it a bit more !


var main = function() {
  $('.article').click(function() {
    $('.article').removeClass('current');
    $('.description').hide();

    $(this).addClass('current');
    $(this).children('.description').show();
  });

  $(document).keypress(function(event) {
    if(event.which === 111) {
      $('.description').hide();

      $('.current').children('.description').show();
    }

    else if(event.which === 110) {
      var currentArticle = $('.current');
      var nextArticle = currentArticle.next();
      
      currentArticle.removeClass('current');
      nextArticle.addClass('current');
    }
  });
}

$(document).ready(main);


#2

Eg you have 10 div's with the class black an in jQuery you select the class black when clicked using this to make it hidden..

Using this will make only the div clicked hidden and not all ten div's with the class black..

If you were to use black then all ten will be hidden when clicked on only one.

So basically this checks which one was clicked and takes action on only that one


#3

ok you mean in event handler "this" only refers to the class clicked ?


#4

Yes      


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.