On that page I have my accordion menu to the left, as you can see it kind of works ok, but if I want to click on a submenu item, to go to that category, it behaves as if it’s closing the accordion. Which is not what I need.
Now I kind of understand why, just trying to think how best to tackle it. Clearly what I have added is not going to work because it’s not specific. I should also point out that the sub list is being dynamically generated and I can’t edit that code to give the anchor a class.
But what I was thinking was can I apply a class to all anchors that are within the ul ul ? And then target that anchor class to prevent it from not following the link?
$(document).ready(function(){
$(".Left #SideCategoryList ul#parent-cats ul:not(:first)").hide();
$(".Left #SideCategoryList ul#parent-cats ul:visible").hide();
$(".Left #SideCategoryList ul#parent-cats li h3 a").click(function() {
$(".Left #SideCategoryList ul#parent-cats li ul:visible").slideUp("slow");
$(this).parent().next().slideDown("slow");
return false;
});
$(".Left #SideCategoryList ul#parent-cats li ul li a").click(function() {
return true;
});
});
By the way, I’m learning JavaScript from scratch again, and making notes and starting to understand the basics more. I take it the transition from that to jQuery is a fairly easy step to make?
Yeah, that’s what I meant. Also just going through the MDC is a good idea once you’re already familiar with the basics. Reading about the Global Objects for example is a good start.
My main trouble is I can’t spend 3 or 4 hours a day learning this as I need to work on projects and so forth - but what I am trying to do is integrate what I know with what I do, and use scripts as a way of understanding.
yeah very true. I’ve always understood variables, and for-loops, and how the basics of a function work - but now I’m learning things like Objects and Methods.
I actually need to use those selectors because the code for the #parent-cats is being reused across the top nav bar.
Not sure what you mean by that. Which selectors, and how are they being reused?
Out of interest, if I want to keep the sub ul open when on a sub cat page how do I do approach that?
You can give it a class of “active” on the server side and then define “active” as being displayed.
Or, you just need to indicate what page you’re on by giving the body a particular class/id. Suppose page you’re on is “products”. You give the body the id “page-products” and each UL sub-category an ID as well (#cat-something). Then you do this:
If this is about the CSS I posted earlier, it should have nothing to do with the jQuery. It’s just to indicate which sub-category should be “active” (whatever that means in your design) when the page loads. Normally it just means it’s highlighted somehow, or no longer a link.
Adding “active” to #parent-cats achieves nothing, as that’s the parent UL that holds all the others.