3

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!

séan35
  • 966
  • 2
  • 13
  • 37

5 Answers5

2

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/

Karl-André Gagnon
  • 33,662
  • 5
  • 50
  • 75
0

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/

KingOfHypocrites
  • 9,316
  • 9
  • 47
  • 69
0

Try this:

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

Also see attributeContainsPrefix selector.

jww
  • 97,681
  • 90
  • 411
  • 885
Sandeep Pal
  • 2,067
  • 1
  • 16
  • 14
0

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']");
Banana
  • 7,424
  • 3
  • 22
  • 43
0

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/

Henri Hietala
  • 3,021
  • 1
  • 20
  • 27