Reputation: 21617
<button class="update vacancy-submit" tabindex="2">Save</button>
$('.vacancy-submit').click(function(){
var type = $(this).attr('class').not('.vacancy-submit');
console.log(type);
});
I'm trying to get the class for button into a variable which should say update
and not update vacancy-submit
Upvotes: 1
Views: 115
Reputation: 93551
After much thinking about this, I would actually suggest you define the following JQuery extension to isolate the problem and make it a bit more reusable:
jQuery.fn.firstClass = function() {
return this.attr('class') ? this.attr('class').split(' ')[0] : "";
};
Then you simply have:
$('.vacancy-submit').click(function(){
var type = $(this).firstClass();
console.log(type);
});
Has test cases for 0, 1 or more classes.
Upvotes: 0
Reputation: 4354
Try this to get the name of first class in class attribute
function classname() {
var myClassName = $('#id').attr('class').split(' ');
var firstName = myClassName[0];
}
Upvotes: 0
Reputation: 318172
If it's always the first class:
$('.vacancy-submit').click(function(){
var type = this.className.split(/\s+/).shift();
console.log(type);
});
Upvotes: 5
Reputation: 388316
You need to replace the unwanted portion of the class attribute value
var type = $.trim($(this).attr('class').replace('vacancy-submit', ''));
Upvotes: 1
Reputation: 6025
The following code will remove vacancy-submit
from the string you get.
$('.vacancy-submit').click(function(){
var type = $(this).attr('class').replace('vacancy-submit','');
console.log(type);
});
Upvotes: 0