Search code examples
jqueryhtmljsp-tags

I need to find if a class exists in a div tag (catch is I only know part of the classname)


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***
});

Solution

  • You can use the attribute-starts-with selector:

    $('.DIV_HORZ > div[class^="R2"]')