Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wildcard in jQuery selector

I'd like to retrieve all elements which start with a specific ID. After a short search, the solution appeared to be:

$('*[id^="foo_"]')

to select all elements with an ID starting with foo_. Although this selector syntax is very logical, I was just wondering whether it is possible to do this in a more 'shorthand' way, like:

$('#foo_*');

This did not work, however. Is there support for wildcarting like this?

like image 647
pimvdb Avatar asked Jan 19 '11 18:01

pimvdb


3 Answers

As far as I know, there's no native way to do this.

However, here is a filter that allows regular expressions to be used for selectors. It should suit you just fine.

like image 198
Chuck Callebs Avatar answered Oct 21 '22 14:10

Chuck Callebs


The selector syntax is almost identical to CSS, in which * means all selectors, rather than a wildcard. There's no need to specify the * in this case: [id^="foo_"] will act in the same way but with slightly less specificity.

like image 30
Rich Bradshaw Avatar answered Oct 21 '22 14:10

Rich Bradshaw


No, it isn't. * is a universal selector meaning "Any element", not a generic wild card. [id^="foo_"] and *[id^="foo_"] are identical (except for specificity considerations).

like image 2
Quentin Avatar answered Oct 21 '22 15:10

Quentin