0

I almost done the css to make a tree view menu. But the problem is the tree would only work with the mouseover. I want to open and close the tree by clicking the link.

My Html is:

<div id="global-nav">
        <ul>
            <li><a href="#">One</a></li>
            <li><a href="#">Two</a></li>
            <li><a href="#">Three</a></li>
            <li><a href='#' class="sidemenu-sub-menu-header">Four</a>
            <ul>
                <li><a href="#">1.2 One</a></li>
                <li><a href="#">1.2 two</a></li>
            </ul></li>
        </ul>
</div>

My Css:

.sidemenu-sub-menu-header { font-weight: bold; color: black;}


#global-nav {font-size: 17px; }
#global-nav ul li{padding: 2px 10px 2px 10px;}
#global-nav ul ul li{ display: none;}
#global-nav li:hover ul li { display: block; }

I am just need a small script to open the menu and close the menu like a toggle bar. Thanks

4 Answers 4

2

That works for you:

$('#global-nav li a').click(function(){

   var nextUl = $(this).next('ul');

   if(nextUl[0] != undefined){
     nextUl.toggle(); //Alternate the visibility
   };

});
Sign up to request clarification or add additional context in comments.

5 Comments

You must edit your css, change #global-nav ul ul li into #global-nav ul ul and remove #global-nav li:hover ul li
i want the <li><a href='#' class="sidemenu-sub-menu-header">Four</a> to close by default and open it when we click the li. Please help me!
U r amazing Dr.Molle. Thank you very much indeed.
It's Renan Cunha's answer, I've only commented
Thanks to both of you. I dont forget to thank him first. Thanks a lot guys. One more small doubt. Is it possible to add a plus and minus image to work accordingly?
2

You can use this jquery plugin to easily achieve what you want http://bassistance.de/jquery-plugins/jquery-plugin-treeview/

Demo http://jquery.bassistance.de/treeview/demo/prerendered.html

Comments

0

CSS Unable to respond click. Better you use Jquery toggle function.

1 Comment

how to add jquery for html? any example..?
0

Look into javascript's handy and (seemingly) widely used onclick function...!

1 Comment

Why did you downvote this and the other very similar comment? If you read forum rules and guidelines a part of posting here you should be posting constructive posts that show at least a little self-motivation and research. We both pointed you in the right direction- go research how to use onclick or toggle, which is in fact the correct way to solve your problem. Please remove the downvotes, as both answers were correct, though we didn't take 10 minutes out of our days to write a script FOR you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.