/    Sign up×
Community /Pin to ProfileBookmark

preventDefault doesn’t stop anchor event

Hi All

I have something like this:

[code=php]$(‘a’).live(‘click’, function(e) {
console.log(“test”);
e.preventDefault();
do_something() ;
//return false ;
}) ;[/code]

When a link is clicked everything goes well, except that I get the page the anchor is refering too, meaning the ‘e.preventDefault();’ doesn’t work
I’ve also tested it with

[code=php]<a href=”http://www.example” onclick=”alert(‘OK’);event.preventDefault();”>sdfsdf</a>[/code]

which works!

Any suggestions how to fix this with jQuery ?

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@Jeff_MottAug 27.2009 — Works good for me.

Note that for newer versions of Firebug, you need to explicitly enable the console and then reload the page. Otherwise you get a JavaScript error "console is undefined", and that error causes the rest of the JS to not execute correctly.
Copy linkTweet thisAlerts:
@jeanlucaauthorAug 27.2009 — Your right about the error (not for console.log), which was not reported by firebug. After fixing it, it worked ?

cheers
×

Success!

Help @jeanluca spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...