Disable link with the prototype observe method
Asked Answered
P

2

10

I want to create a link like this:

<a href="http://example.com">text</a>

and replace the behavior so that the link downloads the content with Ajax instead when clicking.

It is important for me not to replace the href attribute (so copying the link still works).

One solution would be to do:

$('link').onclick = function() { return false; };

but I would like to use the .observe method. But this doesn't work:

$('link').observe('click', function() { return false; });

(which is quite logical).

Any ideas on how I could achieve this?

Thanks.

Protege answered 9/9, 2009 at 13:12 Comment(2)
Looks like you got your answer. The courtesy at this point is to accept one by clicking the large checkbox next to the answer you found most helpful.Seeger
Yeah sorry! I'm new to this. Thanks.Protege
A
4

$('link').observe('click', function(e) { Event.stop(e); });

or

$('link').observe('click', function(e) { e.stop(); });

or

$('link').observe('click', Event.stop);
Athletics answered 9/9, 2009 at 13:20 Comment(1)
Cool... didn't know Event.stop. That's the cleanest solution.Protege
S
11

You have to use the event object to achieve this with prototype.

$('link').observe('click', function(event) { event.stop() });
Schulein answered 9/9, 2009 at 13:19 Comment(0)
A
4

$('link').observe('click', function(e) { Event.stop(e); });

or

$('link').observe('click', function(e) { e.stop(); });

or

$('link').observe('click', Event.stop);
Athletics answered 9/9, 2009 at 13:20 Comment(1)
Cool... didn't know Event.stop. That's the cleanest solution.Protege

© 2022 - 2024 — McMap. All rights reserved.