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,045
  • 14
  • 23
  • 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
  • 354,903
  • 100
  • 647
  • 710
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
  • 112,730
  • 33
  • 157
  • 176
0
$("#buttonId").click(function() { return false; });
Sylvain
  • 3,823
  • 1
  • 27
  • 32
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
  • 57,113
  • 24
  • 136
  • 237