Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Finding a control with only a partial reference ID

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")
like image 426
Mark Avatar asked Dec 16 '11 16:12

Mark


2 Answers

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.

like image 152
biziclop Avatar answered Sep 22 '22 21:09

biziclop


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

like image 21
Mike Fahy Avatar answered Sep 22 '22 21:09

Mike Fahy