Get which div was clicked correctly

Asked

Viewed 451 times

1

I got some Ivs:

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

I’m trying to catch which one was clicked, like this:

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

Only it keeps giving size error: Maximum call stack size exceeded

I wanted every time I clicked on one of these Divs, I would pick which one was clicked on. How do I do this correctly? Or rather, why are you making this mistake?

  • $('.filters__filter'). click(Function(){ Alert($(this).html(); });

2 answers

1


It is almost certain, but the problem is in the inside click, 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 assume that 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.

  • 1

    That’s right, rsrs logic error. When give the time frame as accepted!

  • 1

    You have another error still attention to the class name in HTML and jQuery, it is written differently

  • @Helderpereira, truth I had not noticed it, really the class should have the same name in the tag and in jquery "filter__filters".

1

Basically you’re in the way, the point is that you’re trying to simulate again the click in the same div 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>
<div class="filter__filters tamanho">a</div>
<div class="filter__filters preco">b</div>
<div class="filter__filters cor">c</div>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.