General Agro
General Agro

Reputation: 49

Jquery toggle multiple div with same id

I use jQuery to toggle divs.

<script>
$(document).ready(function(){
    $("#btnstoreofferlisting-3").click(function(){
        $("#storeofferlisting-3").toggle();
    });
});
</script>  

I have more div with "storeofferlisting-3" id. I click to "#btnstoreofferlisting-3" button, but only one div hide and show. How can I make it to working with all same id divs?

Upvotes: 2

Views: 1576

Answers (2)

Kolby
Kolby

Reputation: 2865

First of all, IDs should be unique. You should only have one ID per page. Change the IDs to classes.

Once you do that your code will be something like this:

$(".btnstoreofferlisting-3").click(function(){
    $(this).toggle();
});

https://www.sitepoint.com/javascript-this-event-handlers/

Upvotes: 1

user3754008
user3754008

Reputation: 285

use css class

<div class="myDiv"></div>

<script>
$(document).ready(function(){
    $(".myDiv").click(function(){
        $(".myDiv").toggle();
   });
});
</script>  

Upvotes: 0

Related Questions