Ddd Ddd - 1 year ago 88
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.

Answer Source

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>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download