views:

33

answers:

1

I was advised to used this because i was having a problem, a link worked in FireFox ONLY when clicked the second time. This is to display an external html in a div called leftColumn.

$(function(){
  $('#ulWithAllTheLinks').delegate('li a', 'click', function(e){
     e.preventDefault;
     $('#leftColumn').load(this.href);
  });
});

My question is, that this displays the html with the content in a NEW page, I know that it has something to do with this:

<ul id="one">
  <li><a href="content.html">First Link</a></li>
</ul>

yet i don't know how to link this to the function

+2  A: 

event.preventDefault() is a function, so you need parenthesis on the end, like this:

$(function(){
  $('#ulWithAllTheLinks').delegate('li a', 'click', function(e){
     e.preventDefault();
     $('#leftColumn').load(this.href);
  });
});

Without the .preventDefault() (or return false;) working correctly, the default behavior will occur...going to that page.

Nick Craver
I've always preferred `return false;` - but is the former a more efficient approach?
jakeisonline
@jakeisonline - It depends what you're after, for example if there was another handler above this, like a `$("a").live(...)` then `return false` would stop the event dead in its tracks, preventing the bubble as well, whereas `e.preventDefault()` wouldn't. As a general rule, I use the "lightest" method to do what's needed...it avoids having to track down where the event's stopping...I only stop it if I have good reason to do so. That being said, it's mostly preference, the actual performance difference for an event is infinitesimal.
Nick Craver
@Nick - ah, that makes a lot of sense. I guess I've always just needed to stop the event dead in its tracks.
jakeisonline
Thanks!!! it WAST the (), geez!!
ber
@ber - Welcome :) Be sure to accept answers on this and future questions via the checkmark beside the answer that helped resolve it :)
Nick Craver