I'm using following line and I would like to make it case-insensitive:
var matches = $(this).find('div > span > div#id_to_find[attributeName ^= "filter"]');
if (matches.length > 0) {
}
My question is that how can I make the selector ^=
to be case-insensitive? Maybe changing to filter and then some regexp?
To do a case-insensitive attribute selection, you need to write a custom selector function.
You can use this like this:
This will match any
input
elements whosetype
attribute begins withR
orr
(so it would matchRADIO
,radio
,RESET
orreset
). This is a pretty silly example, but it should do what you need.Re the comment that the function is hard to understand, I'll explain it a little.
This is the standard signature for creating custom selectors.
meta
is an array of details about the call.meta[3]
is the string passed as the parameter. In my example, this istype, r
. The regex matchestype
andr
separately.Return if both these are true:
opts[1] in obj
)I could have made this easier to read using jQuery syntax rather than native JS syntax, but that would have meant reduced performance.
There's a slight problem when using lonesomeday's answer.
To reproduce the error, try this: HTML tag on page is a common Facebook meta tag:
Selector:
This will not work. The reason is because when the selector code gets to the statement
(opts[1] in obj)
it will execute("property" in obj)
which returns false. (I don't know why)To fix this problem, I just changed the last line to use jQuery's attr() method
Here you can see:
http://www.ericmmartin.com/creating-a-custom-jquery-selector/
What you have to do is to create a custom jquery selector:
And then just use it: