1

Assume I have an element like:

<a id="button" data-param1="123" data-param2="456" ... data-paramN="1324" data-anotherName="908678" >Click This</a>

I want to loop in all data having key names including "param" text. For example:

$('a#button').data('param%').each(function(){
   ...
});

Is there anyway to do this?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
serefbilge
  • 1,654
  • 4
  • 29
  • 55

2 Answers2

2

See the .data() function which automatically decodes data-* attributes and returns them in an object: http://api.jquery.com/data/#data

Example: http://jsfiddle.net/ET4ER/2/

$.each( $('a#button').data(), function( key, value ){
    if( key.substr( 0, 5 ) !== 'param') return;
    $('<div>').text( key+'='+ value ).appendTo('body');
});

Result:

paramn=1324
param2=456
param1=123
biziclop
  • 14,466
  • 3
  • 49
  • 65
0

Use :data() selector from jquery ui: http://api.jqueryui.com/data-selector/

Of course you would have to loop through result set and pick the specific ones you want with something like :contains()

cakar
  • 71
  • 1
  • 4