[ask] JQuery preventDefault is not working

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: Jul 2009
Posts: 20
Reputation: eantz is an unknown quantity at this point 
Solved Threads: 0
eantz eantz is offline Offline
Newbie Poster

[ask] JQuery preventDefault is not working

 
0
  #1
Oct 22nd, 2009
Hi,
I want create a page which has animation at the bottom of that page. I use jQuery to do that..

this is the way I do it
  1. <a href="#" class="animation">Animate this</a>

and this is the jquery
  1. $(document).ready(function() {
  2. $('a.animation').click(function(e) {
  3. e.preventDefault();
  4. //animation goes here;
  5. });
  6. });

When I run the code, all animation run very well except the page always scroll to the top. I try to change the href attribute to javascript:void(0); but it still run like that..

How can I fix it?

Thanks before,
Sorry for my English..
learning to live..
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 5
Reputation: nauticalmac is an unknown quantity at this point 
Solved Threads: 0
nauticalmac nauticalmac is offline Offline
Newbie Poster
 
0
  #2
Oct 22nd, 2009
You could do away with your anchor tags and use span instead. Then use CSS to style the cursor to pointer so it shows up as clickable like a link.
Originally Posted by eantz View Post
Hi,
I want create a page which has animation at the bottom of that page. I use jQuery to do that..

this is the way I do it
  1. <a href="#" class="animation">Animate this</a>

and this is the jquery
  1. $(document).ready(function() {
  2. $('a.animation').click(function(e) {
  3. e.preventDefault();
  4. //animation goes here;
  5. });
  6. });

When I run the code, all animation run very well except the page always scroll to the top. I try to change the href attribute to javascript:void(0); but it still run like that..

How can I fix it?

Thanks before,
Sorry for my English..
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 102
Reputation: jomanlk is an unknown quantity at this point 
Solved Threads: 18
jomanlk jomanlk is offline Offline
Junior Poster
 
0
  #3
Oct 22nd, 2009
You can just remove the # from the href (leave it empty or give it a false page url and not an anchor) and return false instead of preventDefault(). Probably not the best way, and your HTML won't validate. But it should fix your issue if HTML validation is not a concern.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 20
Reputation: eantz is an unknown quantity at this point 
Solved Threads: 0
eantz eantz is offline Offline
Newbie Poster
 
0
  #4
Oct 23rd, 2009
Hi all..
I have tried all of your suggestion. But, it didn't work.
But, I just realize that when animations run, actually the page do not scroll to the top of the page. It just scroll to some pixels to the top. But it is disturbing. I'm starting to find where the page actually go when the animations run or may be there is an error in my code.

Thanks all,
I will post it here later if I find it.
But, if someone who has any others suggestion, I hope it will help me..
learning to live..
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 5
Reputation: nauticalmac is an unknown quantity at this point 
Solved Threads: 0
nauticalmac nauticalmac is offline Offline
Newbie Poster
 
0
  #5
Oct 23rd, 2009
I think you need to look elsewhere for the problem. I have tested the following and no scrolling occurs.
html code:
  1. <div id="padding"></div>
  2. <span class="animate">Animate this.</span>
javascript code:
  1. $(document).ready(function() {
  2. $('span.animation').click(function() {
  3. alert("Animate!");
  4. });
  5. });
css code:
  1. #padding {height:1500px;}
  2. .animate {cursor:pointer;}
I've posted a simple page to demonstrate:
Animate Code Test

If you are still having problems, you need to post your other code so we can help.

Originally Posted by eantz View Post
Hi all..
I have tried all of your suggestion. But, it didn't work.
But, I just realize that when animations run, actually the page do not scroll to the top of the page. It just scroll to some pixels to the top. But it is disturbing. I'm starting to find where the page actually go when the animations run or may be there is an error in my code.

Thanks all,
I will post it here later if I find it.
But, if someone who has any others suggestion, I hope it will help me..
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 20
Reputation: eantz is an unknown quantity at this point 
Solved Threads: 0
eantz eantz is offline Offline
Newbie Poster
 
0
  #6
Oct 23rd, 2009
Hi nauticalmac,
Thanks again,
As I said, I think I there is a mistake in my code.
I'm still trying to find it. But, actually I do not find it yet..
learning to live..
Reply With Quote Quick reply to this message  
Reply

Tags
javascript, jquery

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC