Skip to main content

How do you remove a class that matches a pattern from the class attribute, but retain the other classes?



I want to delete the classes that end with 'blue' from the class attribute on all tags





sample html







<p class="text_blue happy">this is blue text</p>

<p class="text_red nothappy">this is red text</p>

<img class="img_blue nothappy" />







This will give me all the elements with classes that end with 'blue'







$('[class$=blue]');







how do I pop these matched classnames off of the class attribute?


Comments

  1. If you're going to be doing this a lot, you might want to change how you assign classes. You can make your HTML like this:

    <p class="text blue happy">this is blue text</p>


    and then instead of using a selector of .text_blue in your CSS, use .text.blue instead. Then you can simply remove "blue" from the classes.

    ReplyDelete
  2. You can pull the whole class name out with a regular expression like this:

    $('[class$="blue"]').each(function() {
    var clsName = this.className.match(/\w*blue\w*/)[0];
    });


    One thing that you should realize is that $('[class$="blue"]') operates on the entire attribute named class. I does not operate on individual class names. So, it will match:

    class="happy text_blue"


    But, it will not match:

    class="text_blue happy"


    because the class attribute does not end with "blue". If you want it to get any class name that contains "blue" regardless of where it is positioned in the class name attribute, you would have to use:

    $('[class*="blue"]').each(function() {
    var clsName = this.className.match(/\w*blue\w*/)[0];
    });


    If you further wanted to filter out class names that did not end with blue, you could do that with JS like this:

    $('[class*="blue"]').each(function() {
    var match = this.className.match(/\w*blue(\b|$)/);
    if (match) {
    var clsName = match[0];
    }
    });


    If you want to remove these class names from the objects, you could do it like this:

    $('[class*="blue"]').each(function() {
    var match = this.className.match(/\w*blue(\b|$)/);
    if (match) {
    $(this).removeClass(match[0]);
    }
    });


    It could also be done this way which seems a little cleaner, but it doesn't perfectly clean up extra whitespace around the class name it's removing:

    $('[class*="blue"]').each(function() {
    this.className = this.className.replace(/\w*blue(\b|$)/, "");
    });

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.