8

After clicking on the link, Click Me, the page scrolls back to the top. I do not want this. How can fix it?

Example: http://jsfiddle.net/Y6Y3Z/

Scroll-bar:

enter image description here

    function myalert() {
        var result = true;
        //var hide = $('.alert').fadeOut(100);
        //var css = $('#appriseOverlay').css('display','none');
        var $alertDiv = $('<div class="alert">Are you sure you want to delete this item?<div class="clear"></div><button class="ok">no</button><button class="cancel">yes</button></div>');
        var link = this;
        $('body').append($alertDiv);
        $('.ok').click(function () {
            $('.alert').fadeOut(100);
            $('#appriseOverlay').css('display', 'none');
            callback(true, link);
        });
        $('.cancel').click(function () {
            $('.alert').fadeOut(100);
            $('#appriseOverlay').css('display', 'none');
            callback(false, link);
        });
        $alertDiv.fadeIn(100);
        $('#appriseOverlay').css('display', 'block');
        return result;
    };
$('.click').click(myalert);
    function callback(result, link) {
        //
        if(result){
        }
    }

3 Answers 3

24

You only need to change the "#" to "javascript:void(0)" on HTML code:

<a href="javascript:void(0)" class="click">Click Me</a>

Another solution is add a "/" after the "#":

<a href="#/" class="click">Click Me</a>
Sign up to request clarification or add additional context in comments.

3 Comments

OMG I love you. That / thing ROCKS!. I just told 5 other developers and Gilberto, you now have 3 development companies kissing your feet right now ^^
or you can simply remove href attribute from there. both will work
2021 and it works perfectly - href="#/" - thank you!
9

The reason that it is going to the top of the page is because your anchor tag has a hash symbol as the href. The hash syntax allows you to refer to a named anchor in the document, with the link taking you to that place in the document. The default action for an anchor tag when you click on it and the href refers to a named anchor that doesn't exist is to go to the top of the page. To prevent this cancel the default action by returning false from the handler or using preventDefault on the event.

function myalert(e) {
    e.preventDefault(); // <-- prevent the default action

    ...

    return false; // <-- alternative way to prevent the default action.
};

4 Comments

Should not use return false; unless you intend to stop event bubbling. See answer by @rodneyrehm instead.
@Sparky672 - absolutely correct, but in nearly all cases of this type of of behavior I've observed preventing event bubbling is also correct.
what is this e here
e is the event object
5

simply prevent the default function (jump to #marker) from executing: http://jsfiddle.net/Y6Y3Z/1/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.