0

What i want to do is stop the clickable link from forwarding the user to the href value as i am calling the value via .load().

Here is my code;

$('#prac_slider li a').click(function(){
    var $permalink = $('#prac_slider li a').attr('href');
    $('div#abc').load($permalink + ' #loadMe');
});

i have tried to add .unbind('click', variable); to the element and loading my above code via a variable but it still seems to forward the user.

Everything else works, it loads the data into #abc but it then forwards the user to the href itself.

How would i disable this?

Xavier
  • 8,244
  • 18
  • 54
  • 85

1 Answers1

2

Use return false:

$('#prac_slider li a').click(function(){
    var $permalink = $('#prac_slider li a').attr('href');
    $('div#abc').load($permalink + ' #loadMe');
    return false;
});

This prevents the default action from being performed, in the case of an a it prevents the link being followed, in the case of a checkbox it prevents un/checking of the box.

You could, similarly, use event.preventDefault():

$('#prac_slider li a').click(function(event){
    event.preventDefault();
    var $permalink = $('#prac_slider li a').attr('href');
    $('div#abc').load($permalink + ' #loadMe');
});

As pointed out by T.J. Crowder, in the comments below, event.preventDefault() and return false are not equivalent: return false is event.preventDefault and event.stopPropagation() (the latter of which prevents the event from bubbling up the DOM tree).

Reference:

Community
  • 1
  • 1
David Thomas
  • 249,100
  • 51
  • 377
  • 410
  • man you are a legend, completely forgot about that ! +1 and will mark within 10 mins or so :) Thanks David – Xavier Jun 11 '11 at 20:44
  • @T.J. Crowder, indeed; that's mainly why I pointed Xavier to Karim79's answer on the linked question. Though, to be fair, I probably should've added that to the answer... **Now edited-in** – David Thomas Jun 11 '11 at 21:04