Regular Expression in data attribute -- jquery

Pat R Ellery picture Pat R Ellery · Oct 12, 2012 · Viewed 8.7k times · Source

I have the current html:

<input id="dynamic1" /* other */ data-group-animal="y1,y2" />
<input id="dynamic2" /* other */ data-group-vegetable="y3,y4" />

These are independent divs that can be use together or not when sending data via ajax.

Now, one option i have is to use both fields and i need to retrieve the data-value for both based on some form options. the data attribute is different (is used for different purposes -- I can send or not all the groups or separately)

so I want to do:

$('input').data('group*') but it does not work, then I realize I need a regexp.

is there a regexp for data attribute i can use?

Answer

Jim Schubert picture Jim Schubert · Oct 12, 2012

You could use selectors if you change your attributes.

http://jsfiddle.net/kvJVM/1/

So, you could have instead a data-group-type and query for that:

<input id="dynamic1" data-group-type="animal" data-group="y1,y2" />
<input id="dynamic2" data-group-type="vegetable" data-group="y3,y4" />​

Some example queries

$('input[data-group]') // all

$('input[data-group-type^="animal"]') // starts with 'animal'
$('input[data-group-type*="l"]') // contains 'l'
$('input[data-group-type!="animal"]') // not 'animal'

There are other selectors: http://api.jquery.com/category/selectors/​​​​​​​​​