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

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex