0
<button class="btn btn-square uppercase bold blue pull-right" type="submit" 
id="button-1"><i class="icon-eye"></i> Ticket Details</button>
<div class="row d-none" id="content-1"></div>

I have to repeat the above code with incrementing the id how should I do that by using this code:

$("#button-1").click(function() {
    $("#content-1").toggle("slide");
});
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
mohanarangan
  • 109
  • 1
  • 11
  • Possible duplicate of [Dynamically setting div id in JavaScript or jQuery](https://stackoverflow.com/questions/7514924/dynamically-setting-div-id-in-javascript-or-jquery) – Sangam Belose Mar 09 '18 at 07:24

2 Answers2

0

This code will do it for id 1 to 10.

for (var i = 0; i < 10; i++) {
    $("#button-" + i).click(function () {
        $("#content-" + i).toggle("slide");
    });
}
pitaridis
  • 2,801
  • 3
  • 22
  • 41
0

First, count all the buttons that you need to create a click function for:

var button_count = $("button").length; //you may also use class here

Then do a loop:

for (var i = 0;i<=button_count;i++) {
    $("#button-"+i).click(function () {
        $("#content-"+i).toggle("slide");
    });
}
tomjosef
  • 895
  • 6
  • 21
  • glad to be of help. i would greatly appreciate if you would mark my answer as accepted or upvote it if it solved your problem. thanks. – tomjosef Mar 21 '18 at 19:13