Reputation: 2549
So, I have a function like this:
$('a.tdt-btn-*').click(function() {
var class = $(this + "[class^=tdt-btn-*]"); // Need more here
console.log(class);
});
What I want to do is get the value of the wildcard part at the end. How could I do this?
Upvotes: 0
Views: 70
Reputation: 135762
Try this (class
is a reserved word, so I'm using clazz
):
$('a[class^="tdt-btn-"]').click(function() {
var clazz = $(this).attr('class').replace('tdt-btn-','')
console.log(clazz);
});
It will take the first occurrence of the tdt-btn-*
.
$('a[class*="tdt-btn-"]').click(function() {
var clazz = $(this).attr('class').match(/(tdt-btn-)(.+?)(?=(\s|$))/)[2];
console.log(clazz);
});
Upvotes: 1
Reputation: 253318
I'd suggest:
$('a[class*=tdt-btn-]').click(function() {
var elClasses = this.className.split(/\s+/),
elClassWildcard;
for (var i = 0, len = elClasses.length; i < len; i++){
if (elClasses[i].indexOf('tdt-btn-') === 0) {
elClassWildcard = elClasses[i].replace('tdt-btn-', '');
}
}
console.log(elClassWildcard);
});
Incidentally, class
is a reserved word in JavaScript and should, or can, not be used as a variable name (I believe an error is thrown if you do so).
References:
Upvotes: 1