jq::attaching click handlers to -all- <a>’s

Wow JQuery really is great ! Say you want to attach click events to all the anchors in your page with classname ‘aa’.

Can be achieved with the following few and simple lines

$(function() {
//attach click event to all anchors of class ‘aa’…
$(“.aa”).click(function() {
alert(“you clicked:” + $(this).attr(‘id’));
//foo($(this).attr(“id”));
})
})

If you wanted to only attach events to anchors within a (named) div, simply replace the $(“.aa”).click(function() { with $(“#yourdiv.aa”).click(function() {.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s