Phorden
Phorden

Reputation: 1034

jQuery - Remove comma from class names

I have divs that have will sometimes have a 2 classes with a comma in between them. I did not code this by hand, it is generated by a webapp. I would like to remove the any commas in the class attribute and replace them with a space. How do I accomplish this? I have multiple classes, so I have to search for that character and replace that character only.

.each(function() {
    var $el = $(this),
        n = $el.attr("class").match(/cat-item-(.*)/)[1];

    $el.addClass(n);
});

Upvotes: 1

Views: 862

Answers (2)

PeterKA
PeterKA

Reputation: 24648

.each(function() {
    var $el = $(this),
          n = $el.attr("class");
    $el.attr('class', n.replace(/,/g,' ') );
});

Upvotes: 0

Lawrence Johnson
Lawrence Johnson

Reputation: 4043

jQuery('whatever_youre_searching_to_update').each(function() {
    var _sCurrClasses = jQuery(this).attr('class');
    jQuery(this).attr('class', _sCurrClasses.replace(/,/g, ' '));
});

Upvotes: 4

Related Questions