Move this topic
Allow * wildcard in .removeClass()
Will not implement
- Maybe later
- Under review
- In-progress
- Implemented
- Will not implement
in Developing jQuery Core
•
8 years ago
It would be nice to be able to use the *
character as a
wildcard in calls to .removeClass()
.
For example, elem.removeClass('foo-*')
would remove
all classes that start with foo-
.
Currently, the easiest way to do this seems to be the following, which is neither elegant nor efficient:
.removeClass(function (_, cl) { return cl.split(' ').filter(function (c) { return c.substr(0, "foo-".length) === "foo-"; }).join(' '); })
1
Replies(15)
Re: Allow * wildcard in .removeClass()
8 years ago
And, you would be the only one who uses these wild cards.
If your site has so many different foo-s. Your logic is
probably flawed.
Most people would keep an array of their foo-s, to remove them.
Or they would not use classes, as
they are not usually used this way. You could simply use a data-foo
attribute instead of a class.
JΛ̊KE
Leave a comment on jakecigar's reply
Re: Allow * wildcard in .removeClass()
8 years ago
No, he's not the only one.
And e.g. Mootools allows
regexps:
el.removeClass('type_[a-z]+');
el.removeClass('type_[a-z]+');
Also, although I do have an array of all possible suffixes, I might
not have them easily accessible here, e.g. in some "function
updateDOMStyling(item)" the item (which contains item.type) could
come via AJAX. The array of possible suffixes exists on the server,
but not necessarily on the client. Still I can easily addClass('type_'
+ item.type) and recognize these styles -- but I can't (currently)
remove them via jQuery...
Leave a comment on thax.jquery's reply
Re: Allow * wildcard in .removeClass()
8 years ago
This would be an interesting plugin. I don’t think it is needed by 99
44/100% of users.
JΛ̊KE
Leave a comment on jakecigar's reply
Re: Allow * wildcard in .removeClass()
8 years ago
http://jsfiddle.net/jakecigar/kfLzY/ defines the plugin declasse. Not
tested in all browsers.
- $.fn.declasse=function(re){
- return this.each(function(){
- var c=this.classList
- for (var i=c.length-1;i>=0;i--){
- var classe=""+c[i]
- if (classe.match(re)) c.remove(classe)
- }
- })
- }
-
- $("p").declasse(/foo-/)
JΛ̊KE
Leave a comment on jakecigar's reply
Re: Allow * wildcard in .removeClass()
8 years ago
Jake, aren’t you being a bit presumptuous there? I know what I’m doing.
I need to use classes because they are used in CSS styling. Using
.data()
and a CSS selector with
[data-foo='...']
does not work, as .data()
does not actually change the attributes as documented.
Leave a comment on Timwi's reply
Re: Allow * wildcard in .removeClass()
8 years ago
What does anything I said have to do with data- attributes?
You can use real data attributes, set with
.attr("data-foo","12") to trigger CSS with selector
[data-foo=’12’] .
I do it all the time!
You just can’t use .data as those values never make it into the DOM.
JΛ̊KE
Leave a comment on jakecigar's reply
Re: Allow * wildcard in .removeClass()
8 years ago
Or I can just use classes, as I’m already doing. That makes the CSS much
simpler. The only thing missing to make the jQuery handling simpler too
is the ability to use a wildcard in
.removeClass()
.Re: Re: Allow * wildcard in .removeClass()
8 years ago
And that’s not going to happen.
So. start writing your own implementation of .removeClass.
JΛ̊KE
Leave a comment on Timwi's reply
Re: Allow * wildcard in .removeClass()
8 years ago
Jake, your behaviour is confrontational and rude. I came here to make an
honest suggestion in good faith and you are responding to it with
disdain. It would be a shame if jQuery’s development is stifled because
of your unwelcoming attitude towards people who are trying to be helpful.
Leave a comment on Timwi's reply
Re: Allow * wildcard in .removeClass()
8 years ago
Sorry you feel that way. I wrote a plugin
that you could use. I know that your need is not common
, I explained why
.
I have submitted a few additions to the core, and I have been
turned down as well. Some of the concepts made it in in other forms,
but most are turned down because they don’t apply to the majority of
users. That’s what plugins are for.
You can wait around to see if one of the active core developers
comes on for another opinion.
JΛ̊KE
Leave a comment on jakecigar's reply
Re: Allow * wildcard in .removeClass()
8 years ago
Thank you for your kind(er) response, Jake.
Personally, I feel you haven’t really explained why the use-case is uncommon:
• You’ve asserted that my “logic is flawed”, but you have no way of knowing this, as you know nothing about my project and its logic. The statement simply assumes that I must be wrong and you must know better, which is confrontational.
• You stated that “most people would keep an array of their foo-s” — you have no way of knowing this without conducting a representative survey. You could have phrased this as a question in order to find out whether that solution is even applicable in my case.
• You ignored thax’s comment which indicates that I’m not alone in this, and which describes a possible reason why your proposed array solution might not be available.
Personally, I feel you haven’t really explained why the use-case is uncommon:
• You’ve asserted that my “logic is flawed”, but you have no way of knowing this, as you know nothing about my project and its logic. The statement simply assumes that I must be wrong and you must know better, which is confrontational.
• You stated that “most people would keep an array of their foo-s” — you have no way of knowing this without conducting a representative survey. You could have phrased this as a question in order to find out whether that solution is even applicable in my case.
• You ignored thax’s comment which indicates that I’m not alone in this, and which describes a possible reason why your proposed array solution might not be available.
I am not upset if the feature is not added to jQuery. There may be a good reason not to, but you haven’t provided it.
Many thanks for your effort in writing that plugin. I’ll definitely consider using that. I admit I didn’t know about .classList.
(P.S. Bulleted lists do not display at all in this forum (at least
according to the preview). I wonder if someone could fix that.)
Leave a comment on Timwi's reply
Re: Allow * wildcard in .removeClass()
8 years ago
Jake was actually polite and helpful. He didn't assert that your logic
was flawed, he said it was probably flawed. You failed to provide any
real details up front, so the best Jake could do was provide a generic
response. He also didn't ignore Thax, he commented that using a regex
would be an interesting plugin, then went ahead and built the plugin for
both of you.
Anyway, Jake is correct that this will not be used by the majority
of users, though every user will pay the price of the implementation.
This isn't going to be implemented in jQuery Core.
Leave a comment on scott.gonzalez's reply
Re: Allow * wildcard in .removeClass()
8 years ago
Thanks Scott.
I’ve been around so long, and I have seen every kind of request.
But I am rarely called names.
JΛ̊KE
Leave a comment on jakecigar's reply
Re: Allow * wildcard in .removeClass()
7 years ago
I know this is an old thread but weighing in on this I think it would be
an incredibly useful piece of functionality. As Thax mentions above -
other libraries (such as mootools) provide a simple way to do this so I
do question why it is too heavy a price to be included in JQuery.
In a project I am currently working on a class with a particular
prefix is added to elements to provide the correct spacing.
For example, Bootstrap's "input-group" element can
contain multiple "input-group-btn"s so a class of
"has-addons-" on the container followed by the number of
addons contained within allows the correct spacing/padding to be
applied via css for other elements or styling.
- <div class="input-group has-addon-1">
- <input type="text" class="form-control" />
- <div class="input-group-btn"></div>
- </div>
-
- <div class="input-group has-addon-2">
- <input type="text" class="form-control" />
- <div class="input-group-btn"></div>
- <div class="input-group-btn"></div>
- </div>
With the ability to use regex or wildcards within removeClass I
could remove/replace all of these classes in one go without the need
for looping through an array.
Whilst I agree I don't think Jake was especially rude I think
you could potentially be easily dismissing some extremely useful
functionality that may have much wider use than expected.
Leave a comment on Daniel Brookes's reply
Re: Allow * wildcard in .removeClass()
7 years ago
I think I will just let this thread fester.
Feel free to start your own thread.
JΛ̊KE
Leave a comment on jakecigar's reply
Change topic type
Link this topic
Provide the permalink of a topic that is related to this topic
Reply to Timwi's idea
{"z39886594":[14737000005494873,14737000005497160,14737000005497210,14737000005497734,14737000005501080],"z2951924":[14737000005495775],"z2656090":[14737000005499109],"z55146647":[14737000006326923],"z2950240":[14737000005497056,14737000005497072,14737000005497074,14737000005497186,14737000005497424,14737000005497740,14737000005501210,14737000006338007]}
Statistics
- 15 Replies
- 27565 Views
- 2 Followers