There is such code:

$(document).ready(function() { $(".portfolio-item").each(function(i) { $(this).find("a").attr("href", "#portfolioModal_" + i); $(this).find(".portfolio-modal").attr("id", "portfolioModal_" + i ) }); }); 

It assigns id links and a modal window, but the fact is that the modal is not in the current (".portfolio-item") , but in the body of the document, that is, this does not work here.

So the question is: how to do that to go back up the DOM in order to indicate the assignment of id modal window.

  • give an example of markup - Grundy

1 answer 1

When using jQuery, you can use the .parent () and .closest () methods. Example:

 $(function() { $(".portfolio-item").each(function(i, el) { let $el = $(el); $el.find("a").attr("href", "#portfolioModal_" + i); $el.find(".portfolio-modal").attr("id", "portfolioModal_" + i); $el.parent(); //будет родитель $el $el.closest('.portfilio_wrapper'); //первый родитель с классом .portfolio_wrapper }); }); 

I also do not recommend using this as a pointer to an element, perhaps it is a taste, but nevertheless this approach is considered a move.