Search code examples
jqueryhtmlhref

Javascript or jquery - is there a way to dismantle an anchor tag


so that it'll only respond to a right mouse click? If the user left clicks on the link I want to just hide it. I can capture right or left mouse clicks through:

$(".hideDataFileLink").live('mousedown', function(e) { 
    if (e.which == 3) { 
        $(this).slideUp('fast');
    } 
});

where 1 = left, 2 = middle, 3 = right. In the end, either click hides the link after the click but this is for downloading a text dump from a database and if you left click on the link you leave the page and load the text in your browser. I don't want that option to happen.

Thanks


Solution

  • Just add return false or e.preventDefault() to stop the browser following the link:

    $(".hideDataFileLink").live('mousedown', function(e) { 
        e.preventDefault();
        if (e.which == 3) { 
            $(this).slideUp('fast');
        } 
        // or return false here, but that will also stop the event from
        // bubbling up to the parent which is needed in some cases
    });