Regular Expression in data attribute -- jquery
Asked Answered
G

1

10

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?

Glaudia answered 12/10, 2012 at 15:32 Comment(3)
There is no support for what you are trying to do. You would probably be better off using a single data attribute and storing json in it representing your structure.Berkeley
@KevinB :( sad, is there one for the value then? for example: data-group="vegetable,animal"? maybe I can change my code to do this insteadGlaudia
Yes, however, .data() can only return the data of a single element, you'll have to loop through your inputs and process their data values if you need them all.Berkeley
O
28

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

Odin answered 12/10, 2012 at 15:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.