1

I have some sliders on the page. Each slider has uniq id.

<div id="gallery-1"><div>
<div id="gallery-2"><div>
........................
<div id="gallery-10"><div>

And i have some functions (JQuery)

 $('#gallery-1').royalSlider({....})
 $('#gallery-2').royalSlider({....})
...............................................
 $('#gallery-1').royalSlider({....})

If i have 10 slider on the page, i have to copy paste 10 fragmets JQuery code (as i have matched above). I want to use Regexp for $('#gallery-[/\d/]'). Can i create something like this, or mb there is another way?

2
  • 2
    There are plugins that add that functionality, but that's the wrong way to fix this problem. Instead, you should be using a common class on these elements, then you can easily select them all with 1 selector. At that point, it might make more sense to separate the id attribute from the id value into it's own attribute so you don't have to do string parsing to get the id of the gallery. Even then, if the id's always go 1-n, you don't need to store that on the element as you could use the elements index within it's parent to get it's index.. Commented Sep 28, 2015 at 15:19
  • Do you know the IDs? If yes, why not simply concatenate in a loop? $('#gallery-'+i)? Or even store all sliders IDs in an array and loop throught this array? $('#'+arrayID[i]) Or use a common class to the sliders as suggested by Kevin B? Commented Sep 28, 2015 at 15:21

2 Answers 2

2

try using:-

$('div[id^="gallery-"]')

this will search for all divs with id's starting with gallery-

Sign up to request clarification or add additional context in comments.

Comments

1

In pure jQuery, you can use this:

$('div[id^=gallery-]').royalSlider({...})

Even though you should really tag those div with some attributes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.