Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Anchor tags list creation

I am creating a list of anchor tags from a MySQL/PHP query; the anchor tags call a JavaScript function.

The 'catch 22' that I have is:

  • href="#" makes the page jump to the top every time one of the anchor tags is clicked (very annoying)
  • removing href="#" means that the cursor does not change as an anchor tag is hovered over nor does that anchor tag have the appearance of an anchor tag.

I know there is a way to handle this with JavaScript (possibly jQuery) but I don't recall how at the moment. However, I really prefer a simpler HTML fix (if one exists) that does not require me to get into JavaScript.

Edit:
"does not require me to get into JavaScript" == "does not require extensive changes in JavaScript."

like image 807
John R Avatar asked Jun 27 '11 15:06

John R


1 Answers

In your javascript function you should return false, or with jquery you can use preventDefault()

Example:

$('a').click(function(event) {
    event.preventDefault();
    // do something
});

Or in your case:

<a href=“#” onclick="foo();return false;">

Or change the href to javascript:void(0):

<a href="javascript:void(0)" onclick="foo();">

Ideally, your link degrades without javascript, so the third option will usually be avoided.

like image 116
Wesley Murch Avatar answered Oct 11 '22 11:10

Wesley Murch