Ddd Ddd - 3 months ago 24
Javascript Question

Can't prevent default link action

I have a function in global scope as a link() handler:

function sort(e, field) {

/* sort process ... */

And I have a link that appears dynamicly in my HTML page:

<a onclick="sort(this,'title')" href="addr" id="sortByTitle">Book title</a>

Despite of e.preventDefault(); the link performs default action anyway.

It's very importamt that my link will be loaded via ajax.


If you must use the onclick attribute, return false; will prevent the default action there.

<a onclick="sort(this,'title'); return false;" href="addr" id="sortByTitle">Book title</a>