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.
In JavaScript, you can call a function or snippet of JavaScript code through the HREF tag of a link. This can be useful because it means that the given JavaScript code is going to automatically run for someone clicking on the link. HREF refers to the “HREF” attribute within an A LINK tag (hyperlink in HTML).
This is a type of JavaScript link - the onclick attribute defines a JavaScript action when the 'onclick' event for the link is triggered (i.e. when a user clicks the link) - and there is a URL present itself in the onclick attribute.
In short: . bind() will only apply to the items you currently have selected in your jQuery object. . live() will apply to all current matching elements, as well as any you might add in the future.
The anwer is: not possible.
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.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With