Answer the question
In order to leave comments, you need to log in
How to track which element was clicked?
Hello.
There are 8 elements on the page, each of them has a block to which you need to add a class when clicked.
here's what I've come up with
<div class="block delivery2">
<img src="img/oplata/delivery2.png" alt="">
<div class="hover"></div>
<div class="block-hover"></div>
<div class="block-click"></div>
</div>
var block = $('.block'),
blockClick = $('.block-click');
block.click(function (e) {
var target = e.target;
blockClick.removeClass('open');
for (var i = 0; i < block.length; i++) {
if (target === block[i]) {
blockClick[i].addClass('open');
}
}
})
Answer the question
In order to leave comments, you need to log in
You don't need a cycle. This is how it should be:
$(e.currentTarget).find('.block-click').addClass('open');
$('.block').children().click(function (e) {
$(this).parent().find('.block-click').addClass('open');
});
I ended up doing a similar solution
$('.block').on('click', function () {
$('.block-click').removeClass('open');
$(this).find('.block-click').addClass('open');
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question