Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop the jump when clicking on an anchor link? [duplicate]

Is there a way of avoiding the jump when clicking on an anchor link? So that the view does not change.

like image 896
Tomkay Avatar asked Jan 31 '12 11:01

Tomkay


2 Answers

The most semantic and meaningful approach to this problem would be to handle the onclick event from within JavaScript. Ideally this file would be best to be stored in a seperate file, however, including a in-line script within your problem file would suffice. Here's how i'd recommended approaching this problem if your already using a JavaScript library like jQuery.

Assign an ID
Include an id attribute to your anchor so it's able to be selected using jQuery:

<a href="#anchor" id="mylink" title="Title Here">Link Text</a>

Bind click event
From within your JavaScript file / in-line script include the following:

$('#mylink').click(function(event) {

    // This will prevent the default action of the anchor
    event.preventDefault();

    // Failing the above, you could use this, however the above is recommended
    return false;

});

The method above is explained in full using the jQuery API websites: http://api.jquery.com/event.preventDefault/

like image 134
James Avatar answered Sep 20 '22 22:09

James


Just use:

<a href="javascript:void(0);">Text</a>
like image 38
Nirmal Avatar answered Sep 20 '22 22:09

Nirmal