I was asked to get rid of Jquery and write all the code on js, the problem is that I do not really understand how I can replace $(this) . for example

 $('.tag').click(function(){ $(this)...etc }) 

It is easy to replace everything with QuerySelect and addEventListener , but $(this) not clear.

  • 3
    This will remain this, only without a dollar with parentheses. He for you for some reason does not work? - andreymal

2 answers 2

 let tag = document.querySelector('.tag'); tag.addEventListener('click', () => { console.log(tag); }); 

And if the item is not one

 let tag = document.querySelectorAll('.tag'); tag.forEach(item => { item.onclick = () => { console.log(item); } }); 

    Just had to do as follows:

     test.addEventListener('click', (event) => { console.log(event.target); //это и будет объектом this }); 
    • @VladiuslavKim and now click on the element that is a child of .tag and see the result ... - Pavel Mayorov
    • @PavelMayorov eh, did not notice that the switch function. Different, but in the absence of child elements (it did not seem to be about the question - although the opposite was not stated, yes) should coincide - Regent
    • @Regent as experience shows, even if initially there were no children there - one day they suddenly appear - Pavel Mayorov
    • @PavelMayorov agree. With event.currentTarget problems like there should not be? - Regent
    • one
      @Regent yes, there should be no problems with currentTarget - Pavel Mayorov