Monday, March 26, 2012

Jquery wildcard selector

It is useful in jquery to be able to use a wildcard in a selector for an #id

If you want the wildcard to match the beginning of a string the code is as follows...


$("[id^=something]")

If you want the wildcard to match the end of a string the code is as follows...

$("[id$=something]")

So the first would select any id that began with "something"
eg:
id="something1"
id="something2"
id="something3"

and the second would seect any id that ended with "something

eg:
id="1something"
id="2something"
id="3something"

This is very handy when there are multiple date pickers on a page, and also in many other situations where #ids may vary, but you want the same code to apply to all without having to specify each #id.

No comments: