1

I have 3 divs within a div container, I loop through each of them. I need to check if the classname matches a certain value. The catch is I know only part of the class name.

e.g.: I just used star wars as an example to illustrate my case. All i know is my classname should be R2XX how do I retrieve R2D2 and R2D5 from the 3 mentioned below

<div class="DIV_HORZ"> --container div
    <div class="R2D5"></div>
    <div class="C3P0"></div>
    <div class="R2D2"></div>
</div>

javascript part:

$(".DIV_HORZ > div[id]").each(function(){
 ***find div whose class matches R2 + something***
});
Haran Murthy
  • 341
  • 2
  • 10
  • 30
  • regex is your friend! check this thread: http://stackoverflow.com/questions/190253/jquery-selector-regular-expressions – nebulae Jul 27 '12 at 23:21

2 Answers2

6

You can use the attribute-starts-with selector:

$('.DIV_HORZ > div[class^="R2"]')
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
0

In addition to the starts with selector, you could also use your .each() loop and use your own custom code including a regular expression if you wanted:

$(".DIV_HORZ > div[id]").each(function(){
    if (this.id.match(/^R2/) {
        // found a matching id, process the item here
    }
});

The regular expression or code can obviously be as involved as you want it to. If needed, it can use more logic that just the built-in selector engine has.

jfriend00
  • 683,504
  • 96
  • 985
  • 979