Ankur
Ankur

Reputation: 51100

How to pass a variable from a link to a jQuery function

I would like a jQuery function to know which link was clicked to call it, i.e. I would like the link's id value to be passed to the jQuery function.

Is this possible? If so what is the neatest way to do it.

Upvotes: 0

Views: 3080

Answers (4)

saleem ahmed
saleem ahmed

Reputation: 337

$("a").click(function() {
  alert($(this).attr("id"));
  ...
});

i can say this same..

Upvotes: 0

Vladimir Kocjancic
Vladimir Kocjancic

Reputation: 1844

Don't forget to cancel default behaviour, or you won't achieve nothing.

$("a").click(function(e) {
   e.preventDefault();
   var linkid = $(this).attr("id");
   //do whatever here
});

Upvotes: 1

Philippe Leybaert
Philippe Leybaert

Reputation: 171734

$("a").click(function() { 
   var linkid = $(this).attr("id");

   // use linkId here
});

Upvotes: 2

cletus
cletus

Reputation: 625027

Sure. Inside the click() event handler you can refer to the element clicked by this.

$("a").click(function() {
  alert(this.id);
  ...
});

or

$("a").click(function() {
  alert($(this).attr("id"));
  ...
});

Upvotes: 4

Related Questions