Correctly pick which div was clicked

1

I have some divs:

<div class="filter__filters tamanho"></div>
<div class="filter__filters preco"></div>
<div class="filter__filters cor"></div>

I'm trying to get which was clicked, like this:

$j('.filters__filter').click(function(){
    $j(this).click();
});

Only getting size error: Maximum call stack size exceeded

I wanted every time I clicked one of these divs, I would pick which one was clicked. How do I do this correctly? Or rather, why are you giving this error?

    
asked by anonymous 13.10.2017 / 15:06

2 answers

1

This is almost right, but the problem is in the click from inside, you click and then call the event click again inside:

$j('.filter__filters').click(function(){
    $j(this).click();
});

You have to define what you want to do, for example, let's suppose you want to get the text then:

$j('.filter__filters').click(function(){
    alert($j(this).text());
});

So you will get the text of the div that was clicked.

    
13.10.2017 / 15:09
1

Basically you're in the way, the point is that you're trying to simulate click again in the same div as previously clicked.

$(document).on('click', '.filter__filters', function(){
  $('.filter__filters').css('background', 'white')
  $(this).css('background', 'red')
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divclass="filter__filters tamanho">a</div>
<div class="filter__filters preco">b</div>
<div class="filter__filters cor">c</div>
    
13.10.2017 / 15:09