Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
43
rated 0 times [  48] [ 5]  / answers: 1 / hits: 22899  / 13 Years ago, wed, november 23, 2011, 12:00:00

I've got a page with some questions and answers, the answers are collapsed by default. When they click the question I expand the hidden answer-div. The problem is that when I click these questions, the window jump to the top of the screen. This is not a huge problem, but I find it annoying, because I have to scroll down to the question again.



The links simply looks like this:



<a href=# id=myID>Myquestion</a>


And I've used jQuery and .click as event-listener.



Are there any simple ways to avoid this, or do I have to use .scroll and finding the coordinates of the question? I'd rather avoid this.



EDIT: I know that I can use anchors to do this, but I'd like to avoid any jumping of the screen at all.


More From » jquery

 Answers
30

You need to add preventDefault() to your click handler. This will stop the browser executing it's own link handler, and will only run the code you specify.



Example:



$(#myID).click(function(e) {
e.preventDefault();
// Do your stuff
});

[#88945] Tuesday, November 22, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
moriah

Total Points: 201
Total Questions: 100
Total Answers: 82

Location: Tuvalu
Member since Sun, Sep 4, 2022
2 Years ago
;