Gath
Gath

Reputation: 1345

How to bind jQuery to ONE link <a> to an onclick function?

How do I bind a function using jQuery to one and only one link label in my HTML document which has several links?

My code looks like this;

$("a").click(function(){
  $("#login").slidedown("slow");
});

But this binds all the links in the document.

Upvotes: 6

Views: 25430

Answers (2)

Jeremy B.
Jeremy B.

Reputation: 9216

To expand on Michael, you'll want to add a return false;

$("#clicked_link").click(function(){
    $("#login").slidedown("slow");
    return false;
});

Otherwise when you click the link the browser will still attempt to follow the link and you'll lose the javascript action.

Upvotes: 14

Michael
Michael

Reputation: 780

Name your anchor/link that has the click event with an id attribute.

So for instance:

<a href="..." id="clicked_link">...</a>

And then use the following jquery statement:

$("#clicked_link").click(function(){ $("#login").slidedown("slow"); });

Easy as pie!

Upvotes: 13

Related Questions