0

I am using the following code to call JS function for mentioned DIV #lightGallery

 $("#lightGallery").lightGallery({
    thumbnail: false,
});

I need modifing JS code to make function be called for any #DIV+num like #lightGallery1, #lightGallery2,...etc

B Faley
  • 17,120
  • 43
  • 133
  • 223

3 Answers3

2
$('[id^="lightGallery"]').lightGallery({
    thumbnail: false,
});
Tomanow
  • 7,247
  • 3
  • 24
  • 52
1

Instead of this, I would simply give the elements a class Name and call all of them with

$('.lightGallery').lightGallery({
    thumbnail: false,
});
baao
  • 71,625
  • 17
  • 143
  • 203
  • This is a better way to do this. You grab by Id when you just want 1 element, you grab by class when you want multiple elements. – mattfetz Jan 29 '15 at 22:10
0

At its simplest, use attribute-starts-with selector for the id:

$("[id^=lightGallery]").lightGallery({
    thumbnail: false,
});

If you need to filter out other elements whose id begins with lightGallery, but follow with non-numeric characters, you can also use filter():

$("[id^=lightGallery]").filter(function(){
    return /^lightGallery\d+/.test(this.id);
}).lightGallery({
    thumbnail: false,
});

References:

David Thomas
  • 249,100
  • 51
  • 377
  • 410