click handler on a link

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
Live24x7
Forum Contributor
Posts: 194
Joined: Sat Nov 19, 2011 9:32 am

click handler on a link

Post by Live24x7 »

While I could manage to use a handler on a submit button - it is not working on links
How do i add a click handler on a link .
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: click handler on a link

Post by pickle »

Using just raw Javascript? jQuery? MooTools?

How are you adding your handler to the button?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Live24x7
Forum Contributor
Posts: 194
Joined: Sat Nov 19, 2011 9:32 am

Re: click handler on a link

Post by Live24x7 »

sorry - i am still fiddling with all these - but the case in question is jQuery
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: click handler on a link

Post by pickle »

Then it's pretty much exactly as easy as adding a listener to the button:

Code: Select all

$("select the links").on('click',function(){
    //do stuff here
    
    //return false if you want don't want the link to be followed
});
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Live24x7
Forum Contributor
Posts: 194
Joined: Sat Nov 19, 2011 9:32 am

Re: click handler on a link

Post by Live24x7 »

so if write something like this

[text]$("http://localhost/sytest/usr/register.php").on('click',function(){
alert("Registrations Closed for this month");

});[/text]

and include it in header along with the jQuery source , and then every time i click on a link pointing to the above URL, it should give this alert ?

Is that correct ? or am i missing something ?
It is not working that way so i assume i am missing something.

Thanks for your replies.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: click handler on a link

Post by pickle »

You're missing something. You need to have a proper selector: http://api.jquery.com/category/selectors/

In this case, the selector could be something like:

[syntax]a[href$="register.php"][/syntax]
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Live24x7
Forum Contributor
Posts: 194
Joined: Sat Nov 19, 2011 9:32 am

Re: click handler on a link

Post by Live24x7 »

thanks for that selector link.

still not working but i guess will have to do some more reading before getting my hands dirty with the actual code
Post Reply