15

is it possible to search for the a control on a webform, where you just want to match "anything" for part of it ie:

I have various buttons on a page - which take the id format: ID#(id1)#(id2)#(date) - for example:

ID-8258-3103-2011-12-18
ID-8258-3104-2011-12-18
ID-8258-3105-2011-12-18

I want to pass the (id1) and (date) in a querystring, and then have jQuery search for the first control on the page, which matches the (id1) and (date) and any (id2) - so for example, I pass 8252 and 2011-12-18 - so how would I find the first control above, from jQuery?

$("#ID-8252-????????-2011-12-18")
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
Mark
  • 7,778
  • 24
  • 89
  • 147

3 Answers3

37

Instead of using the #id selector, you can combine multiple attribute selectors, which filters the id attribute (one for the beginning of the id, one for the end):

$('[id^="ID-8258"][id$="2011-12-18"]').addClass('found');

To select only the first match, use the :first selector (not the same as the :first-child selector!)

$('[id^="ID-8258"][id$="2011-12-18"]:first').addClass('found');

See http://api.jquery.com/category/selectors/ for reference.

Example:

http://jsfiddle.net/wbLZ8/3/

It seems that these selectors can be used even with IE7, if you want to use them in plain CSS.

biziclop
  • 14,466
  • 3
  • 49
  • 65
  • It is possible for it to find the *first* control, and then stop after that? Thanks again, Mark – Mark Dec 19 '11 at 15:14
1

You'll want to use a filter in your selector, as discussed in jQuery selector regular expressions. (Check nickf's response for an example).

Community
  • 1
  • 1
Mike Fahy
  • 5,487
  • 4
  • 24
  • 28
0

I found this to be most helpful jquery-wildcard-selectors-some-simple-examples

Serj Sagan
  • 28,927
  • 17
  • 154
  • 183