I will go straight to the example ..

There are several such blocks:

<div class="caption"> <p>тут описание данного блока</p> <a href="#" class="open-dec">Подробнее</a> </div> 

Clicking on the "Details" link opens the contents of the <P> in the pop-up magnificPopup window - I am writing this jQuery code:

 var $link = $(".open-dec"), $self = $link.index(); //здесь по идеи номер элемента, но не знаю как получить var service_api = $('.caption p:eq('+$self+')').text(); $link.magnificPopup({ items: { src: '<div class="white-popup animated zoomInUp">'+service_api+'</div>', type: 'inline' } }); 

$self variable returns "-1" - according to the idea there is an element number, such as 0, 1, 2, 3, etc.

    1 answer 1

    The sequence number is not required if the position of the elements in the block is known and it does not change:

     $('body').on('click', '.open-dec', function() { console.log($(this).prev('p').text()); }); 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="caption"> <p>тут описание данного блока 1</p> <a href="#" class="open-dec">Подробнее</a> </div> <div class="caption"> <p>тут описание данного блока 2</p> <a href="#" class="open-dec">Подробнее</a> </div> <div class="caption"> <p>тут описание данного блока 3</p> <a href="#" class="open-dec">Подробнее</a> </div> <div class="caption"> <p>тут описание данного блока 4</p> <a href="#" class="open-dec">Подробнее</a> </div> 

    • Thank you for your reply and attention! - Maqsood
    • Please see what I get from this: jsfiddle.net/3n9quwko - Maqsood
    • The script does not work from the first click and when you click on other links, it first opens the test of the previous block .. - Maqsood
    • one
      @Maqsood, I gave the example of a click event, how to get through to the text, but in your case you need to go like this jsfiddle.net/3n9quwko/1 - MasterAlex