user837593
user837593

Reputation: 357

How to attach click events on selected links for a page using jQuery?

Hi I am using following code to attach event to links on the page.

$(document).ready(function(){ 
     $("a").click(function(){
     ..... do something
   });
  });

But I don't want to do this for all the links. I want to do this for few selected links. Can anyone help how can I achieve this?

Upvotes: 0

Views: 72

Answers (4)

Adrian J. Moreno
Adrian J. Moreno

Reputation: 14859

Add a class to the links you want to affect, then change your selector to $('a.className').

Upvotes: 0

Patrick Desjardins
Patrick Desjardins

Reputation: 140843

$(document).ready(function(){ 
     $(".className1").click(function(){     /*Action1*/   });
     //or
     $("#id1").click(function(){     /*Action2*/   });
  });

Upvotes: 0

Litek
Litek

Reputation: 4888

Refine your selector - instead of all links, target them by class, id, or parent etc.

$(document).ready(function(){ 
 $(".myClass1, .myClass2, #myId").click(function(){
 ..... do something

}); });

Upvotes: 0

wanovak
wanovak

Reputation: 6127

Add a class to all of the links you want to add click events for, then:

$('.thatClassName').click(function(){ ... });

Upvotes: 3

Related Questions