jmenezes
jmenezes

Reputation: 1926

jQuery bind click to data attribute instead of class

In jQuery, can I bind a click event to a data handle instead of a class? I'm not sure how to word it, but this is what I'm trying to do...

<a data-user="jason">

$(data-user).click(function() { //This is just for example

Right now, I have it this way, but it's causing a lot of problems with other things. Can I use data instead of a class?

<a class="user">

$(".user").click(function() { //This is just for example

Upvotes: 0

Views: 228

Answers (2)

Use attribute equals selector

$('a[data-user="jason"]').click(function() { .... }

Updated after OP's comment.

$('[data-user]').click(function() { .... }

Upvotes: 4

Kamal
Kamal

Reputation: 2522

Try

$('a[data-user]').click(function() {});

Upvotes: 1

Related Questions