tags:

views:

39

answers:

3

I have a list as such :

<ul id="yearMenu">
  <li>Select a year</li>
  <li><a href="AJAX/2010PR.html">2010</a> |</li>
  <li><a href="AJAX/2009PR.html">2009</a> |</li>
  <li><a href="AJAX/2008PR.html">2008</a> |</li>
  <li><a href="AJAX/2007PR.html">2007</a> |</li>
  <li><a href="AJAX/2006PR.html">2006</a> |</li>
  <li><a href="AJAX/2005PR.html">2005</a></li>
</ul>

below that in another container is this div:

<div id="tableContent" class="tabWrapp"></div>

and the following load function:

$("#ul#yearMenu li a").click(function() {
  var $parent = $(this).parent();
  $parent.addClass("selected").siblings().removeClass("selected");
  var href = $(this).attr('href');
  $("#tableContent").load(href + " #tableContent ", function() {
    //need to do something here?
  });
});

but all links open in another page. what do I need to do to get this to load in the #tableContent div?

+4  A: 

Try adding

return false;

to the end of your "click" handler. Without that, the default action of each <a> tag will be taken.

It would be even better (safer, some would say) to call .preventDefault() on the event object:

$('#whatever').click(function(evt) {
  // ...
  evt.preventDefault();
});

That'll allow the event to bubble but prevent the <a> default action.

Pointy
@Pointy, works great, thanks.
Dirty Bird Design
+1  A: 

Don't use href to store the link. use a custom attr like 'data-link="http://link.com"'

<li><a data-link="AJAX/2010PR.html">2010</a> |</li>

so, when you click nothing will happen.

In your javascript code, you get the link using:

$(selector).attr("data-link");

Then to makeup the A as link, use some css.

It's just a alternative way, but with "preventDefault()" or "return false", also works well.

madeinstefano
This will work, but it might cause crawlers/spiders to miss the links, which for some sites would be a problem.
Pointy
@Pointy, exactly!
Dirty Bird Design
+1  A: 

Try this:

$("ul#yearMenu li a").click(function(event) {
  var $parent = $(this).parent();
  $parent.addClass("selected").siblings().removeClass("selected");
  var href = $(this).attr('href');
  $.get(href, function(data) {
    $('#tableContent').html(data);
  });
  event.preventDefault();
});
cambraca
@cambraca - initially I used the above, adding return false(); However the .load didn't work in IE. I tried yours using the .get and it worked perfectly. Thanks man.
Dirty Bird Design
@cambraca - after further testing I get this to work in all browsers, except Safari, it doesn't load any scripts with the page being loaded. I'm loading a table that uses .jquery tablesorter and pagination plug in. When I click a link to load a different table, no JS functionality again this is only in Safari. I have placed the links to the JS on both the main page and the AJAX page, as well with the functions. any ideas?
Dirty Bird Design
hmm maybe you can put the link to your page here? seeing the code may be helpful
cambraca