3 Replies - 973 Views - Last Post: 17 March 2013 - 03:52 PM

#1 V0R73X  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 12
  • Joined: 01-December 11

Prevent anchors from redirecting to the top of the page

Posted 17 March 2013 - 11:28 AM

Hello,
I'm using some html links as buttons, for example:
<a href="#" onclick="foobar()">Click!</a>

However, whenever I click the link (since it's an anchor) it sends me to the top of the page, then performs the function.
How can I prevent it from sending me to the top of the page?
Is This A Good Question/Topic? 0
  • +

Replies To: Prevent anchors from redirecting to the top of the page

#2 V0R73X  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 12
  • Joined: 01-December 11

Re: Prevent anchors from redirecting to the top of the page

Posted 17 March 2013 - 11:44 AM

Hello?! I thought these forums were popular...
Was This Post Helpful? -3
  • +
  • -

#3 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3489
  • View blog
  • Posts: 10,057
  • Joined: 08-June 10

Re: Prevent anchors from redirecting to the top of the page

Posted 17 March 2013 - 01:14 PM

View PostV0R73X, on 17 March 2013 - 07:28 PM, said:

How can I prevent it from sending me to the top of the page?

most simple way is by not using an anchor (<a>) element. second simple way is not using the anchor’s href attribute.
Was This Post Helpful? 0
  • +
  • -

#4 FrankC  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 45
  • Joined: 06-October 11

Re: Prevent anchors from redirecting to the top of the page

Posted 17 March 2013 - 03:52 PM

View PostV0R73X, on 17 March 2013 - 11:28 AM, said:

Hello,
I'm using some html links as buttons, for example:
<a href="#" onclick="foobar()">Click!</a>

However, whenever I click the link (since it's an anchor) it sends me to the top of the page, then performs the function.
How can I prevent it from sending me to the top of the page?


Assuming you need the function to be executed, add 'return false', like this:
<a href="#" onclick="foobar(); return false">Click!</a>

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1