A
A
Artem Pavliienko2018-01-29 22:32:35
JavaScript
Artem Pavliienko, 2018-01-29 22:32:35

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');
            }
        }
    })


please tell me what is the problem?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
K
kulaeff, 2018-01-29
@kvazarmp

You don't need a cycle. This is how it should be:

$(e.currentTarget).find('.block-click').addClass('open');

V
Vladislav, 2018-01-30
@ddddasass

$('.block').children().click(function (e) {
$(this).parent().find('.block-click').addClass('open');
});

A
Artem Pavliienko, 2018-01-30
@kvazarmp

I ended up doing a similar solution

$('.block').on('click', function () {
        $('.block-click').removeClass('open');
        $(this).find('.block-click').addClass('open');
    });

Thanks to all.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question