1

Is it possible to unbind the default behavior of a hyperlink?

I've learned from other questions that unbind can only be used on functions being bound with jQuery. I guess this could be the reason for it not working.

jsFiddle

Tim
  • 1,017
  • 14
  • 22
  • do you mean the href attribute? see this: http://stackoverflow.com/questions/179713/how-to-change-the-href-for-a-hyperlink-using-jquery – Stefan Ernst Mar 23 '11 at 15:26

4 Answers4

3

You can prevent the default behavior using event.preventDefault, or by returning false from an event handler.

$('#some-link-id').click(function (event)
{
    event.preventDefault();
});

// or

$('#some-link-id').click(function ()
{
    return false;
});

Demo →

As you suspected, you cannot use unbind() because there is no listener to unbind.

Matt Ball
  • 344,413
  • 96
  • 627
  • 693
3

$('myDiv').unbind('click') - if it was hooked up via JavaScript

$('myDiv').attr('onclick','') - if it has an inline event

$('myDiv').attr('href','javascript://') - creates a dummy link that does nothing

Diodeus - James MacFarlane
  • 110,221
  • 32
  • 151
  • 174
0
$("#buttonId").click(function() { return false; });
Sylvain
  • 3,793
  • 1
  • 26
  • 31
0

you could try to bind all event types to it and use event.preventDefault() to stop default behavior something like this:

$('a').bind('touchstart touchmove touchend mousedown click mouseup select',function(e){
e.preventDefault()
})
Billy Moon
  • 54,763
  • 23
  • 128
  • 231