Suppose I have the elements as below:
<div class="home">
<div class="tab231891230"></div>
<div class="tab121232441"></div>
<div class="tab123134545"></div>
</div>
How can I use jQuery to select the div element that the class starts with "tab"
?
You can do it like this:
See http://api.jquery.com/attribute-starts-with-selector/
It is called the Attribute Starts With Selector. My example sets a red text color on the elements:
jsFiddle Demo
Please note that if the elements have more than one class and the other precedes the one with
tab
inside (class="nyedva tab231891230"
) the element won't be selected by this selector.If you want to select even these, you can use this example:
jsFiddle Demo
If you have multiple class inside one element, use this to select
It will work with element like this
Reference : jquery attribute-contains selector
why use that? that number, you can assign to rel or id attribute, like this:
then it will be accessible at:
or even, add a subclass of that current "tab" class:
then, just select by "tab" class like in the jQuery example above, and do whatever you want with the second class (check if it's there, remove it).
check these:
http://api.jquery.com/class-selector/
http://api.jquery.com/hasClass/
http://api.jquery.com/addClass/
http://api.jquery.com/removeClass/