views:

22

answers:

3

How do I write a jQuery selector that will find elements that have string1 anywhere in its ID, and also string2 anywhere in its ID?

+3  A: 

you mean this? attribute contains selector.

Description: Selects elements that have the specified attribute with a value containing the a given substring.

example,

HTML

<input id="man-news" />
<input id="milkman" />
<input id="letterman2" />
<input id="newmilk" />

jQuery

$("input[id*='man']").val("has man in it!");
$("input[id*='man'][id*='letter']").val("has man and letter in it!");​

result: fiddle

Reigel
@Reigel - That's what I'm currently using, but I'm not sure how to use it to match two seperate strings?
ben
you mean [this](http://fiddle.jshell.net/XbzCH/)?
Reigel
+1  A: 

You can chain together the contains selector

$("span[id*='first'][id*='second']") // selector for any span with "first" and "second" anywhere in the id

Working example --> http://jsfiddle.net/mtgNE/

Jamiec
+1  A: 
$('div[id*=foo][id*=blah]');

would look for foo and blah at all available divs.

Example: http://www.jsfiddle.net/XuKND/

jAndy