Abram
Abram

Reputation: 41904

Jquery to select DIV by iterated ID

I would like to select each of these divs separately. Here is what I am trying:

HTML:

<div id="options_sort1"></div>
<div id="options_sort2"></div>
<div id="options_sort3"></div>

Script:

for (var i = 1; i <= 3; i++){
  var $div = $('[id ^=options_sort][id $=counter]');
}

Upvotes: 0

Views: 34

Answers (3)

Syed Muhammad Zeeshan
Syed Muhammad Zeeshan

Reputation: 1045

try this :

for (var i = 1; i <= 3; i++){
    var div = $('#options_sort' + i);
}

Upvotes: 1

PeterKA
PeterKA

Reputation: 24638

Use the following:

for (var i = 1; i <= 3; i++){
  var $div = $('#options_sort' + i);
}

Upvotes: 3

H&#233;ctor
H&#233;ctor

Reputation: 509

Use this:

for (var i = 1; i <= 3; i++){
 var div = $('#options_sort'+i);
}

Don't forget to remove $ of "$div" and add jQuery to your code;

Upvotes: 1

Related Questions