'How to get elements of specific class starting with a given string?
I have a list of elements that have multiple classes, for example:
<input class="etape btn-info others">
<input class="etape btn-inverse others">
<input class="etape btn-danger others">
How to write jQuery-code that will allow me the following...
$(".etape").click(function(){
$(this).get("the class that starts with btn-")
// in order to store this value in a variable to reuse it later
});
Solution 1:[1]
You can use Regular Expression or split the class name.
$(".etape").click(function(){
var classes = $.grep(this.className.split(" "), function(v, i){
return v.indexOf('btn') === 0;
}).join();
});
Solution 2:[2]
You can also try:
$(".etape").click(function () {
var theClass = $(this).attr("class").match(/btn[\w-]*\b/);
console.log(theClass);
});
Uses match instead of grep...
Solution 3:[3]
// Only select input which have class
$('input[class]').click(function(){
var myClass;
// classNames will contain all applied classes
var classNames = $(this).attr('class').split(/\s+/);
// iterate over all class
$.each(classNames, function(index, item) {
// Find class that starts with btn-
if(item.indexOf("btn-") == 0){
// Store it
myClass = item;
}
});
});
Solution 4:[4]
It would probably be better to store those values in the data attribute:
<input class="etape others" data-btntype="info">
<input class="etape others" data-btntype="inverse">
<input class="etape others" data-btntype="danger">
Then:
$(".etape").click(function(){
var myBtnType = $(this).data('btntype');
});
Solution 5:[5]
You could just make btn its own class. However, this will work.
$("div[class^='btn-']")
Solution 6:[6]
Try this: $('input[class*='btn']').attr("class");
Solution 7:[7]
I just made @Vohuman 's function to reusable one :
// getClassStartsWith(classes,startswith);
// Example : getClassStartsWith( 'item w-2 h-4 hello' , 'h-' );
function getClassStartsWith(t,n){var r=$.grep(t.split(" "),function(t,r){return 0===t.indexOf(n)}).join();return r||!1}
Example...
HTML :
<div class="item w-2 h-4 hello"></div>
JS :
var $element = $('.item');
var classes = $element[0].className;
var getHeight = getClassStartsWith(classes,'h-');
That will return h-4, And if no class starts with h- will return false
Solution 8:[8]
How about this? http://api.jquery.com/attribute-contains-word-selector/
$('input[class~="btn-"]')
Solution 9:[9]
A class starting with btn and which can not be the first class:
Working fiddle: http://jsfiddle.net/c9Tdx/
$("input[class^='btn'],input[class*=' btn']").each(function(){
//whatever
});
Solution 10:[10]
Try this,
if($(selector).attr("class").lastIndexOf("classToStartFrom") == 0)
return "this selector class name starts with 'classToStartFrom'";
else
return "this selector class name doesn't start with 'classToStartFrom'";
The code is not tested.
Solution 11:[11]
This function can get Class, ID, Data and all kind of attributes using regex
$.fn.Get_Attr_Regex = function(Pattern,Type)
{
Type = Type || 'class';
var List_Str = this.attr(Type);
if(typeof List_Str !== typeof undefined && List_Str !== false)
{
var regex = new RegExp(Pattern, "g");
var List_Array = List_Str.split(" ");
for(var i = 0; i < List_Array.length; i++)
{
if(regex.test(List_Array[i]))
{
return List_Array[i];
}
}
}
return false;
};
To use it
$('.T_Header').Get_Attr_Regex('btn.*','class'); // => return class value which start with btnxxx
or
$('.T_Header').Get_Attr_Regex('btn.*','id'); // => return id value which start with btnxxx
or
$('.T_Header').Get_Attr_Regex('btn.*','data-info'); // => return data attribute value which start with btnxxx
Solution 12:[12]
In case someone want it to achieve on page load instead of clicking the element, use following code:
var classes = '';
jQuery(".etape").each(function() {
classes += $.grep(this.className.split(" "), function(v, i) {
return v.indexOf('btn') === 0;
}).join() + '|';
});
console.log(classes);
Replace "|" with other symbol that you want to join the class names with.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
