views:

18

answers:

1

I have a panel hide/show setup that is triggered from a list item anchor. Works great. The problem is, I also have a dropdown menu in the site navigation that also needs to trigger each panel, from any page on the site. Like say, I have panels (in this order), "Hardware", "Software", "Accessories" on the Products page. So when I'm on the About page, and I click "Software", the expected result is to take me to the Products page and have the "Software" panel open. Here's my jquery script for the panel:

$("ul#product-type li a").click(function() {
$("ul#product-type li").removeClass("selected");
$(this).parent().addClass("selected");
var currentTab = $(this).attr("href");
    $("div.panel").hide();
    $(currentTab).show();
return false; });

Dropdown Nav Markup:

<ul id="nav">
<li><a href="/products/#hardware"><span>Hardware</span></a></li>
<li><a href="/products/#software"><span>Software</span></a></li>
<li><a href="/products/#accessories"><span>accessories</span></a></li>

I've tried adding the nav selector to the click function, but that doesn't work. Any ideas or suggestions would be hugely appreciated.

Cheers, Ryan

A: 

The href isn't a valid selector, since $("/products/#hardware") wouldn't work. However using the .hash property is perfect, as you'll get $("#hardware"). Use it like this:

$("ul#product-type li a").click(function() {
  $("ul#product-type li").removeClass("selected");
  $(this).parent().addClass("selected");
  $("div.panel").hide();
  $(this.hash).show();
  return false; 
});
Nick Craver
Thank you Nick! That worked great. my click function now looks like:$("#nav li ul li a, ul#product-type li a").click(function() {Do you think there is a way keep the "selected" class working when the #nav link is clicked? Right now, it just removes the "selected" class from the ul#product-type li and adds it to "nav li ul li".
Ryan Palmer
@Ryan - I'd change `$("ul#product-type li").removeClass("selected");` to `$("ul#product-type li").not($(this).parents()).removeClass("selected");` to exclude the parent chain if that's what you're after.
Nick Craver