There are many elements with one CSS class. How to choose the item on which the page visitor clicked? Example:

HTML:

<div class="class"> <a id="aclass">клик</a> </div> <div class="class"> <a id="aclass">клик2</a> </div> 

Javascript:

 $('.class').click(function() { var link = $('.aclass',this).text(); alert(link); }); 

This code is probably wrong, since it does not work.

    1 answer 1

    Use this to refer to the selector that was clicked.

     $('.class').click(function() { var link = $(this).find('.aclass').text(); alert(link); }); 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="class"> <div class="aclass">Link 1</div> </div> <div class="class"> <div class="aclass">Link 2</div> </div> 

    PS You have aclass in html specified as id , and in js you are trying to get it as a class .

    • Thanks, it works))) ehh but I didn’t notice at all =) - mega94