1

This finds the id but does not remove the class

$('[id^=paritalIDname]').removeClass('[class^=partialClassName]');

Probably because the element looks like

<div id="partialIDname-full" class="something someone partialClassNameFull">

Whether the element has a class name of partialClassNameFull or partialClassNameHalf I need to remove it.

I thought I could use a wildcard in the class selector, like

removeClass('[class^=partialClassName*]');

but that's not working.

What is a good solution? (Thanks.)

6
  • You can't do pattern matches on class names. Instead, arrange for there to be two classes: "partialClassName" and "partialClassNameFull". Commented Jun 14, 2012 at 0:15
  • Hey... using the first line of code in your question and the info you can glean from this answer to a similar question (stackoverflow.com/a/57819/414972) can you get something that will work? Commented Jun 14, 2012 at 0:20
  • 1
    I would use filter() with a quick regex like $els.filter(function(){ return /partialClassName/.test(this.className) }) Commented Jun 14, 2012 at 0:21
  • If there is only two classes and you want them removed why not just use removeClass('partialClassNameFull partialClassNameHalf') whichever class is present will be removed. Commented Jun 14, 2012 at 0:25
  • @Musa it's more complicated than that... Commented Jun 14, 2012 at 1:33

2 Answers 2

1

This will handle all partial match.

        $("div[id^=partialId]").each(function () {
            var cls = $(this).attr("class").split(" ");

            for (var i in cls) {
                if (/partialClass/.test(cls[i])) {
                    $(this).removeClass(cls[i]);
                    break;
                }
            }
        });
Sign up to request clarification or add additional context in comments.

1 Comment

Yep, and that's what I needed - "all" partial matches. Thanks Jules! (BTW, you remove the break and return below to make sure you get through the for loop.) -- but you knew that. :)
0

You need to explicitly remove both classes:

$('[id^=partialIDname]').removeClass('partialClassNameFull').removeClass('partialClassNameHalf');

Because .removeClass() only works on full class name matches. If one of the class names is not present, then nothing will happen - no error is thrown when you try to .removeClass() a class that is not present.

You can also try out as suggested in the comments the briefer version of:

$('[id^=partialIDname]').removeClass('partialClassNameFull partialClassNameHalf');

2 Comments

You could improve this slightly: $('[id^=partialIDname]').removeClass('partialClassNameFull partialClassNameHalf');
If it were only two class names then I would not bother asking the question. The need for the solution arose when building a multi-form page that manipulated the DOM extensively in order to ease the user into the required information being sought. The ability to remove several classes by calling a filtered class name made it easier to handle the various stages.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.