Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery On Click with data attribute [duplicate]

Possible Duplicate:
Selecting element by data attribute

I'm trying to listen to when an element with a certain data attribute is clicked but I can't seem to get the on click working and I'm sure its something easy on my part that I'm missing. I have

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

I have that with variations. My question is, how can I use an data attribute with on click?

like image 715
Devin Dixon Avatar asked Nov 18 '12 23:11

Devin Dixon


3 Answers

Easy solution to your problem: (Not tested)

$('a[data-spinner="true"]').click(function(event) {

});
like image 52
Robin Jonsson Avatar answered Oct 11 '22 13:10

Robin Jonsson


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

    $( "[data-spinner]" ).live( "click", function () {
        console.log('clicked');
    } );
like image 20
Akhil Sekharan Avatar answered Oct 11 '22 13:10

Akhil Sekharan


The following code binds click event to all <a> elements which have data-spinner attribute equal to true:

$("a[data-spinner='true']").click(function() {
    //Do Acction
});
like image 12
VisioN Avatar answered Oct 11 '22 11:10

VisioN