My Link How to determine ...">

How to define a link using custom attributes using jQuery?

I have some links like this:

<a href="#" track="yes">My Link</a> 

How to determine when a link with the track attribute is clicked?

Thanks!

+4
source share
2 answers
 $("a[track]").click(function() { ... }); 

This will attach a click event to each link with the track attribute.

An even better solution is to use live to limit the number of event handlers:

 $("a[track]").live("click", function() { ... }); 
+6
source

Use the attribute selector:

 $("a[track]").click(function(e){ // Your code }); 

Example: http://jsfiddle.net/jonathon/uXwSF/

As andre points out in the comments, if you want to get only links where track='yes' , then do:

 $("a[track='yes']").click(function(e){ // Your code }); 

If you want to get all the links with the track attribute, but know what that value is:

 $("a[track]").click(function(e){ var shouldTrack = $(this).attr('track'); }); 
+7
source

Source: https://habr.com/ru/post/1332477/


All Articles