Reputation: 322
I have a div with class tab-detector one active
and this function:
$(".tab-detector").click(function (e) {
var classList = $(e.target).attr('class');
classList.replace("tab-detector","");
classList.replace("active","");
alert(classList);
});
I supposed that it should alert "one", but I see "tab-detector one active" instead - .replace()
didn't change anything.
What am I doing wrong?
Upvotes: 2
Views: 286
Reputation: 240908
The .replace()
method doesn't alter/mutate the initial string. You still need to assign the returned value back to the initial string.
Therefore it should look something like:
$(".tab-detector").click(function (e) {
var classList = $(this).attr('class');
classList = classList.replace('tab-detector', '').replace('active', '');
alert(classList);
});
or:
$(".tab-detector").click(function (e) {
var classList = this.className.replace('tab-detector', '').replace('active', '');
alert(classList);
});
If your intent is to remove those classes, you should really be using the .removeClass()
method instead, since that's exactly what it's designed for.
$(".tab-detector").click(function (e) {
$(this).removeClass('tab-detector one');
});
Upvotes: 2