JQuery on click with data attribute

Possible duplicate:
Item selection by data attribute

I am trying to listen when an element with a specific data attribute is clicked, but I can’t get it to work, and I’m sure that for my part, something is easy, that I’m missing. I have

<a href="/home" data-spinner="true" /> 
 $.data('record').click(function() { //Do Action }); 

I have it with variations. My question is: how can I use the data attribute with a click?

+7
source share
3 answers

A simple solution to your problem: (not verified)

 $('a[data-spinner="true"]').click(function(event) { }); 
+26
source

This selects all elements with the data-spinner attribute, regardless of the attribute value.

  $( "[data-spinner]" ).live( "click", function () { console.log('clicked'); } ); 
+6
source

The following code associates the click event with all <a> elements that have the data-spinner attribute equal to true :

 $("a[data-spinner='true']").click(function() { //Do Acction }); 
+5
source

All Articles