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

Slow Android emulator

I have a 2.67 GHz Celeron processor, 1.21 GB of RAM on a x86 Windows XP Professional machine. My understanding is that the Android emulator should start fairly quickly on such a machine, but for me it does not. I have followed all instructions in setting up the IDE, SDKs, JDKs and such and have had some success in staring the emulator quickly but is very particulary. How can I, if possible, fix this problem?

CCNA 1 Final Exam 2011 latest (hot hot hot)

  Hi! I have been posted content of ccna1 final exam (latest and only question.) I will post the answer and insert image on sunday. If you care, please subscribe your email an become a first person have full test content. Subcribe now  Some question  have not content because this question have images content. So that can you wait for me? SUNDAY 1. A user sees the command prompt: Router(config-if)# . What task can be performed at this mode? Reload the device. Perform basic tests. Configure individual interfaces. Configure individual terminal lines. 2. Refer to the exhibit. Host A attempts to establish a TCP/IP session with host C. During this attempt, a frame was captured with the source MAC address 0050.7320.D632 and the destination MAC address 0030.8517.44C4. The packet inside the captured frame has an IP source address 192.168.7.5, and the destination IP address is 192.168.219.24. At which point in the network was this packet captured? leaving host A leaving ATL leaving...