Hello! There is such a construction:

<div id="wrappep"> <div id="id1"> <a href="#">Бсылка 1</a> <a href="#">Бсылка 2</a> </div> <div id="id2"> <a href="#">Бсылка 3</a> <a href="#">Бсылка 4</a> </div> </div> 

How to use Javascript or jQuery to make it so that when you click on a link, the value of the identifier of the <div> block is written to the variable?

For example, when you click on Бсылка 1 , the variable var link assigned the value id1 And when you click on Бсылка3 var link will be equal to id2

This must be done using the onclick method.

  $('#wrapper').click(function (event) { // ΡƒΠ·Π½Π°Π΅ΠΌ ΠΏΠΎ ΠΊΠ°ΠΊΠΎΠΌΡƒ элСмСнту Π±Ρ‹Π» ΠΊΠ»ΠΈΠΊ // присваиваСм Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ ΠΏΠΎ id }) 

Thank!

    1 answer 1

    The .parent() function can help.

     var link; $('#wrappep').on('click', 'a', function (event) { link = $(this).parent().attr('id'); console.log(link); }) 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="wrappep"> <div id="id1"> <a href="#">Бсылка 1</a> <a href="#">Бсылка 2</a> </div> <div id="id2"> <a href="#">Бсылка 3</a> <a href="#">Бсылка 4</a> </div> </div>