jQuery hyperlinks – href value? text][1]
I am running in to a problem using jquery and a click event attached to an anchor element.
[1]: jQuery hyperlinks – href value? “This” SO question seems to be a duplicate, and the accepted answer doesn’t seem to solve the problem. Sorry if this is bad SO etiquette.
In my .ready() function I have:
jQuery("#id_of_anchor").click(function(event) { //start function when any update link is clicked
Function_that_does_ajax();
});
and my anchor looks like this:
<a href="#" id="id_of_anchor"> link text </a>
but when the link is clicked, the ajax function is performed as desired, but the browser scrolls to the top of the page. not good.
I’ve tried adding:
event.preventDefault();
before calling my function that does the ajax, but that doesn’t help.
What am I missing?
Clarification
I’ve used every combination of
return false;
event.preventDefault();
event.stopPropagation();
before and after my call to my js ajax function. It still scrolls to the top.
That should work, can you clarify what you mean by “before”? Are you doing this?
Because that should work in the sense that if it’s not working YOU are doing something wrong and we’d need to see more code. However, for the sake of completeness, you could also try this:
EDIT
Here is an example of this working.
The two links use this code:
And as you can see they are working just fine.