Ddd Ddd - 24 days ago 8
Javascript Question

Can't prevent default link action

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

function sort(e, field) {
e.preventDefault();

/* 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.

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

Answer

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>