Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression in data attribute -- jquery

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?

like image 253
Pat R Ellery Avatar asked Oct 12 '12 15:10

Pat R Ellery


1 Answers

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/​​​​​​​​​

like image 53
Jim Schubert Avatar answered Oct 17 '22 08:10

Jim Schubert