views:

387

answers:

3

Hello,

I am currently working on building a very simple site for an open source research project for my University. I am using JQuery to animate the sub-navigation of the site. However, my code only seems to work in IE and not Firefox or Chrome.

I'm not sure if it is a compatibility issue, or if it is my fault. I looked up examples on the web, and I do not see any differences in code as to why mine will not work.

The HTML for the section of the site is as follows :

<!-- START NAVIGATION & SUB NAVIGATION -->
<div class="nav">
    <ul>
     <li><a class="nav_home" href='#'><span>home</span></a></li>
     <li><a class="nav_about" href="#"><span>about</span></a></li>
     <li><a><span>research</span></a></li>
     <li><a><span>findings</span></a></li>
     <li><a><span>contact</span></a></li>
    </ul>
</div>
<div class="sub_nav">
    <ul class="sub_nav_home">
     <li><a><span>sub link</span></a></li>
     <li><a><span>sub link</span></a></li>
     <li><a><span>sub link</span></a></li>
     <li><a><span>sub link</span></a></li>
     <li><a><span>sub link</span></a></li>
     <li><a><span>sub link</span></a></li>
     <li><a><span>sub link</span></a></li>
    </ul>
    <ul class="sub_nav_about">
     <li><a><span>sub link</span></a></li>
     <li><a><span>sub link</span></a></li>
     <li><a><span>sub link</span></a></li>
     <li><a><span>sub link</span></a></li>
     <li><a><span>sub link</span></a></li>
     <li><a><span>sub link</span></a></li>
    </ul>
</div>
<!-- FINISH NAVIGATION -->

**Note: this is just testing information to make sure I can get the navigation working before implementing the real thing. Also, only the first two links work. I didn't see the need to implement them all until I got it working.

And the JavaScript is as follows :

var current_sub = 0;

$(document).ready(function() {
    //hide elements
    $("div.sub_nav > ul").hide();

    function get_sub_navigation(nav_name)
    {
     if(current_sub != 0)
     {
      $(current_sub).fadeOut("slow", function ()
      {
       $(nav_name).slideDown("slow");
      });
     }
     else
     {
      $(nav_name).slideDown("slow");
     }
     current_sub = nav_name;
    }

    $("a.nav_home").click(function(event)
 {
  event.preventDefault();
  get_sub_navigation("ul.sub_nav_home");
 }
);
$("a.nav_about").click(function(event)
 {
  event.preventDefault();
  get_sub_navigation("ul.sub_nav_about");
 }
);
});

Ok, so the first thing is does is hide all sub nav lists. Then I have a listener for two of the nav links that should call get_sub_navigation. That function will check if there is one showing (just used 0 for default/nothing) and if there is hide it and if not, then show one.

As you can see, it is not finished code but, I don't want move any further until I can get this figured out.

Any help is much appreciated!

+2  A: 

Call event.preventDefault() in your event handler, not $(nav_name).preventDefault() in your navigation function (or pass the event into it). I suspect that the default is not getting prevented, and the page is being redrawn.

T.J. Crowder
ok, I have edited the code as edited above, but it is still not behaving as expected. I have uploaded the site to my webpage at http://splitside.net/nku/. You can look at what it's doing in Firefox and Chrome and how it should act in IE (i'm using 6 for css testing purposes)
John
"medium" is not listed as an option for the speed parameter - http://docs.jquery.com/Effects/fadeOut#speedcallback Could that be it?
T.J. Crowder
that was actually in the comment below and I changed all values to "slow" to be correct and consistent.
John
When I looked at it when posting that comment, they were still "medium". Now it looks like you're in the middle of updating or something...
T.J. Crowder
even still, I can't get it to work. I have no idea.
John
and yeah, I'm sorry. I didn't mean to sound mean. I just wrote you back that I had already tried it and then realized that I hadn't updated the question yet. So you were right, it hadn't changed when you saw it. Thanks though!
John
A: 

First of all, to hide the ul elements by default on page load you can simply use css instead of javascript:

div.sub_nav > ul { visibility: hidden; }

(i'm not 100% IE <= 6.0 will be 100% OK with this selector, though)

Second, and while i am by no means a jQuery expert, i would imagine that since the selector returns multiple elements it needs a different approach.

try

$("div.sub_nav > ul").each( function() {
    $(this).hide();
});
jcinacio
that works, however I would like to animate the hide() by replacing it with hide("medium") however, when I try this I am stuck with the same problem.
John
AFAIK, the first (speed) argument should be one of: ("slow", "normal", or "fast")
jcinacio
well, I changed everything to slow to be correct and consistent, but it's the same problem as before.
John
A: 

Alright, I got the ul's to show in Firefox and chrome. The problem was that you called current_sub before defining it. That would cause it to fail. I think this should take care of it.

    $(document).ready(function() {
 //hide elements
 $("div.sub_nav ul").hide();

 function get_sub_navigation(nav_name)
 {
 current_sub = nav_name;
 display = $(current_sub).css("display");
    if(display == "none")
  {
    $(current_sub).slideDown("slow");
  }
  else
  {
    $(current_sub).fadeOut("slow");
  }

 }

 $("a.nav_home").click(function(event)
  {
    event.preventDefault();
    get_sub_navigation("ul.sub_nav_home");
  }
);
 $("a.nav_about").click(function(event)
  {
    event.preventDefault();
    get_sub_navigation("ul.sub_nav_about");
  }
);
});
Alex Morales
This isn't making a lot of sense to me, maybe you could clarify. If I'm reading the code correctly this is what it's saying. First set all ul's to hide. (good so far) Then when, say home, is clicked it prevents the default (still good) and then calls get_sub_nav and passes it the nav name(still good). Then if it is set to display:none show it (good) and if it is visible, hide it. This is my problem. If you set current_sub to the one you just clicked and there is a ul already present, it will not go away. Can you please clarify.
John
the problem isn't getting the ul's to show, it's getting them to go away when I want to pull another one in.
John
Sorry I haven't gotten back sooner. I've been out sick. I think changing $(current_sub) to $(nav_name) should take care of that issue. That way you can set whatever you want to current_sub and it will still hide. I'm not sure I'm understanding the problem you're having with the code. Maybe you could share more on what you're trying to do?
Alex Morales