collapse

i have a list items, i want the nested items to collapse by default and on click should toggle as hide and show. please help.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type='text/javascript' src='http://code.jquery.com/jquery-1.7.1.js'></script>
</head>
<body>
<ul>
<li><a href="#">Menu1</a>
  <ul>
    <li>Sub menu 1</li>
    <li>Sub menu 2</li>
  </ul>
</li>
<li><a href="#">Menu2</a>
  <ul>
    <li>Sub menu 1</li>
    <li>Sub menu 2</li>
  </ul>
</body>
</html>

Open in new window

codelevelAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jessica WhitleySoftware DeveloperCommented:
http://www.howtocreate.co.uk/tutorials/jsexamples/listCollapseExample.html
A very basic approach is something like this. You approach it basically as a tree.
You could also make it more eye appealing and use bootstrap.
Just google bootstrap to see the functions available for their framework.
codelevelAuthor Commented:
jessica

the example u gave is outdated. looking for simple hand written script
<script src="collapsibleList.js" type="text/javascript"></script>
Jessica WhitleySoftware DeveloperCommented:
http://code.stephenmorley.org/javascript/collapsible-lists/

You can download 2 different versions here.
Jessica WhitleySoftware DeveloperCommented:
RobOwner (Aidellio)Commented:
here is a simple demo using jQuery's slideToggle function: http://jsbin.com/tumofa/edit?html,css,js,output

I've changed the markup slightly so that individual elements can be identified:

<!DOCTYPE html>
<html>
<head>

<script src="https://code.jquery.com/jquery-1.11.3.js"></script>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
<ul id="menu">
<li><a href="#">Menu1</a>
  <ul>
    <li>Sub menu 1</li>
    <li>Sub menu 2</li>
  </ul>
</li>
<li><a href="#">Menu2</a>
  <ul>
    <li>Sub menu 1</li>
    <li>Sub menu 2</li>
  </ul>
</body>
</html>

Open in new window


#menu > li ul {
  display: none;
}

Open in new window


$(function() {
  $("#menu > li > a").on('click', function(e) {
    $(e.target).next().slideToggle();
  });
});

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.