tags:

views:

62

answers:

2

I have a page that's content is already inside a tab, and don't want to use a tab inside a tab. There will be 4 years 2010, 2009, 2008, 2007 that are anchors across the top of the #content div. When you click a year, it should load the specific content via jQuery's ajax load functionality into the div#content. That is easy enough. Is it possible to make a click function that will hide whatever is currently visible and display the appropriate content?

 $("a#foo").click(function(){
    $("#Year10").load("2010.php #content");
    $("#Year09, #Year08, #Year07").hide();
  });

I guess what I'm asking is it possible to make it hide anything that currently is in the #content div and show the appropriate div? Would this be better with the content being external pages or div's that are hidden on load?

thx

A: 

I would give the tabs an .active class and specify the name (say 2010?) attribute.

$(".tab").click(function () {
    $(".active").removeClass("active");
    $(this).addClass("active");
    $(this).load($(this).attr("name") + ".php");
});

Then have a CSS mapping for .active that indicates visible, otherwise .tab is display: none;.

Josh K
@Josh K so using this would require that the content to be loaded have the same name as the link (minus the extension) and these would be hidden div's on the same page correct?
Dirty Bird Design
@Dirty: Correct.
Josh K
+2  A: 

Nope, you're on the right track.

Assuming you had links like these

<ul id="menu">
    <li><a href="2010.php">2010</a></li>
    <li><a href="2009.php">2009</a></li>
    <li><a href="2008.php">2008</a></li>
</ul>

And a content div

<div id="content"></div>

You just need to write a simple jQuery function.

$("#ul#menu li a").click(function(e) {
    // Prevent going to the page
    e.preventDefault();

    // store the parent (li)
    var $parent = $(this).parent();

    // add class of selected on parent li, and remove it from any other elements
    $parent.addClass("selected").siblings().removeClass("selected");

    // get href
    var href = $(this).attr('href');

    $("#content").load(href + " #content", function() {
        // do something after content is loaded.
    });
});
Marko
@Marko - I like this Marko, thank you. Could you explain the "store the parent LI" it just makes the one clicked run the code correct? So the third line adds the class "selected" to the one you just clicked and removes it from any that might have it correct? So how does this remove the content from the #content div when you change selection?
Dirty Bird Design
@Dirty Bird Design: Because `.load()` will replace the `#content` div with whatever the response from the AJAX call is.
Marko
@Marko - can't get it to load content into the div, it just opens it in another page
Dirty Bird Design
If anyone else needs this, the code works but has 2 errors. remove space btwn " #content" and add a return false; outside click function
Dirty Bird Design
Hi @Dirty Bird Design: I noticed I forgot to put e.preventDefault() to prevent going from the page, I've now updated my code above. And as per the jQuery docs, there **should** be a space between the href and content id (i.e. `example.com #content`). Was this not the case with your example?
Marko
Hi @Marko - the return false / e.preventDefault() (I admit I dont really know which is better) seemed to do the trick. Ill try again with the space, I removed it and it worked in FF, which may have been due to the return false(); and removing the space may be what caused it to not work in IE. I had to go with .get to get it to work in IE. Have you ever had any issues with .load in IE, to this point I have not. as far as the space, I was talking about the space here "space#content"
Dirty Bird Design
@Marko - Got your code working in FF and Safari OSX, but I can't get it to work on WIN IE. Until then Ill have to go with the .get method, this is disapointing as I hate not getting things to work as they should. thx man.
Dirty Bird Design
@Dirty Bird Design - can you post your full code on http://jsFiddle.net ? `.load()` works for me in all browsers.
Marko
@Marko here's the URL, i forgot how to include other plug ins on jsFiddle and not sure these are hosted anywhere. http://www.dirtybirddesignlab.com/about#NinjaTrader-News like I said it works in all browsers except IE, if you load the page, you see how the table should look, then click "2009" and it doesn't load the content, but does perform the alert in the click function. Also, in Safari, it's not adding the JS functionality thx man.
Dirty Bird Design
Thanks @Dirty Bird Design, can you please put a space inside `" #tableContent"`? The jQuery example's all have a space and I wonder if it's related. Otherwise your code looks OK, try stripping some of the other Javascript functions from the page and see what happens. Lastly, you may want to append your domain name before the HREF, so something like `var href = "http://www.dirtybirddesignlab.com/" + $(this).attr('href');` I would try all the other suggestions before appending the domain, since I've had it working with relative URLs before.
Marko
@Marko, I added the space, it still fires the alert but no content is added
Dirty Bird Design
OK I got it, the reason it's not loading is because the hash value you're passing to `.load()` (in this case #content) is supposed to be an ID or Class Name of an element on the page you're loading *i.e. 2010PR.html). I see that you're loading the `<table class="schedule">` from each file, so just replace `$("#content").load(href + " #content"` with `$("#content").load(href + " .schedule"`.
Marko
@Marko, yeah I figured that out, I added another div wrapper around the content on the page i'm loading (but like yours better since I won't have to modify the page, they all have .schedule) but either way its still not allowing any JS functionality once content is loaded.
Dirty Bird Design
Ok try one last thing. Add a slash before each href so it looks like `/AJAX/2010PR.html` and remove any trailing spaces after your selector. `href + " .schedule "` should be `href + " .schedule" `.
Marko
still no js functionality is this a ".live" issue?
Dirty Bird Design
I've run out of ideas mate :/
Marko
no worries, i think I have some research to do. It probably has to do with the ".live" function and loading scripts along with the content. I appreciate your help man.
Dirty Bird Design
@Marko, FWIW i got it to work by including the jquery functions and links inside the body of the AJAX pages, and not loading a specific div, but the whole page of the AJAX pages. Probably not the best but seems to work in Safari now. thx again.
Dirty Bird Design