séan35
séan35

Reputation: 1052

how to remove classes ending with some characters from elements

I got elements with the javascript code below;

var elems = $("*[class*='-highlight datepick-other-month']");

I want to remove classes end with "-highlight" such as; red-highlight, blue-highlight and etc, from "elems".

How can I do this? All feedbacks will be appreciated!

Upvotes: 3

Views: 1825

Answers (5)

Sandeep Pal
Sandeep Pal

Reputation: 2175

Try this:

 $( "a[hreflang|='en']" ).css( "border", "3px dotted green" );

Also see attributeContainsPrefix selector.

Upvotes: 0

Henri Hietala
Henri Hietala

Reputation: 3041

You can remove those classes using regexp:

$.each($("*[class*='-highlight']"), function() {
  $(this)[0].className = $(this)[0].className.replace(/\b.*-highlight\b/g, '');  
});

Here is a demo: http://jsfiddle.net/wzQFT/

Upvotes: 0

Karl-André Gagnon
Karl-André Gagnon

Reputation: 33870

You can use this code :

elems.attr('class', function(_, old){
    return $.grep(old.split(/ +/), function(v){
         return !v.match(/-highlight$/);
    }).join(' ');
})

Working fiddle : http://jsfiddle.net/zvcCL/

Upvotes: 2

Banana
Banana

Reputation: 7463

here is how you select elements that end with a keyword: jQuery Ends With

and here is how you select elements inside elems:

$([what],[where])

so you would want to do this:

var elems = $("*[class*='-highlight datepick-other-month']");
$("*[class$='-highlight']",elems).remove();

this is how you remove the elements from the document.
if you want however, to exclude those elements from your elems var, you can just exclude them as follows:

var elems = $("*[class*='-highlight datepick-other-month']").not("[class$='-highlight']");

Upvotes: 0

KingOfHypocrites
KingOfHypocrites

Reputation: 9537

You need to use the ends with selector: http://api.jquery.com/attribute-ends-with-selector/

jQuery( "[attribute$='value']" )

Then call removeClass: http://api.jquery.com/removeclass/

Upvotes: 0

Related Questions