Reputation: 4519
I have a div set to toggle hide/show onClick. I currently have it set to trigger a chat feature but when someone clicks it, it brings them back to the very top of the page, or resets the position of where they last where. How can I stop this from happening easily ? Thank you!
SOLUTION: Thank you to everyone who contributed! While all the answers worked, I ended up just removing the href="#" like everyone said since it was the most simplest method. Thanks again guys!
Upvotes: 2
Views: 223
Reputation: 2278
Use:
$('.link').click(function(ev) {
if(ev && !ev.preventDefault()) ev.returnValue = false;
....
as this works as intended on IE<9.
Upvotes: 2
Reputation: 8784
Usually this is because you have href="#"
or something and it is actually going to this anchor and not finding it.
So what you should do is return false;
in your onclick handler. That will prevent the click processing from continuing, thus preventing the browser from navigating to your nonexistent anchor.
Upvotes: 1
Reputation: 21335
Prevent the default action from happening when the link is clicked with jQuery.
$('#theLink').click(function(c) {
c.preventDefault();
});
Upvotes: 4