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?

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. Commented Oct 12, 2012 at 15:35
  • @KevinB :( sad, is there one for the value then? for example: data-group="vegetable,animal"? maybe I can change my code to do this instead Commented Oct 12, 2012 at 15:37
  • 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. Commented Oct 12, 2012 at 15:38

1 Answer 1

28
+50

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.