Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select an element when the class name starts with a certain word

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 elements that have the class starting with "tab"?

like image 479
Charles Yeung Avatar asked May 19 '11 07:05

Charles Yeung


People also ask

How do you find an element with a specific class name?

Use the element. classList. contains() method to check if an element contains a specific class name.

Can we get element by class name?

The JavaScript getElementsByClassName is used to get all the elements that belong to a particular class. When the JavaScript get element by class name method is called on the document object, it searches the complete document, including the root nodes, and returns an array containing all the elements.

How do I find the first element of a class name?

If you want only the first element in the DOM with that class, you can select the first element out of the array returned. var elements = document. getElementsByClassName('className'); var requiredElement = elements[0];

Which selector will finds elements with a specific class?

The class selector selects HTML elements with a specific class attribute.


4 Answers

It is called the Attribute Starts With Selector. My example sets a red text color on the elements:

$('[class^="tab"]').css('color', 'red'); 

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:

$('.home div').filter(function () {     return this.className.match(/\btab/); }).css('color', 'red'); 

jsFiddle Demo

like image 170
kapa Avatar answered Sep 21 '22 01:09

kapa


If you have multiple class inside one element, use this to select

$("[class*='tab']"); 

It will work with element like this

<div class="home"> <div class="module tab231891230"></div> <div class="module tab121232441"></div> <div class="module tab123134545"></div> 

Reference : jquery attribute-contains selector

like image 41
cyberfly Avatar answered Sep 20 '22 01:09

cyberfly


You can do it like this:

$('div[class^="tab"]');

See http://api.jquery.com/attribute-starts-with-selector/

like image 22
Tim Avatar answered Sep 23 '22 01:09

Tim


First and foremost: try always to use a Safe Delimiter like - I.e: tab-something

The proper way to select class by prefix is by using a combination of two Attribute Selectors, the ^ Starts With and * Includes.

The reason being: by using only: [class^="tab-something"] such will select only class="tab-something foo bar" but not class="foo tab-something bar" elements — giving you erratic results.

Target elements by className prefix

JavaScript:

const tabs = document.querySelectorAll('[class^="tab-"], [class*=" tab-"]');

jQuery:

const $tabs = $('[class^="tab-"], [class*=" tab-"]');

CSS:

[class^="tab-"], [class*=" tab-"] {
  /* "tab-" prefixed class styles here */
}

To recap:
the above will make sure to target both "tab-aaa foo bar" and "foo tab-bbb bar"

like image 29
Roko C. Buljan Avatar answered Sep 21 '22 01:09

Roko C. Buljan