Solved

Jquery multilevel expandable menu

Posted on 2010-11-12
7
786 Views
Last Modified: 2012-05-10
Hellow all,

First of all, let me say that I am a very beginner when it comes to Jquery. I am working from the book Novice to Ninja, and building a vertical multilevel sliding menu. All works fine. I have given the  LI elements that i like to have default expanded a classname .active. I'd really like to start the menu with these classnames active/expanden. Is this possible? And how can this be done?

I have attached the code

thanks in advance

greetz,
walter
<!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 src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js" type="text/javascript" charset="utf-8"></script>

<script type="text/javascript">
<!-- menu
	$(document).ready(function(){
	  $('#maincontent_menu ul > li ul')
		.click(function(e){
		  e.stopPropagation();
		})
		/*.filter(':not(:first)')*/
		.hide();
		
	  $('#maincontent_menu ul > li, #maincontent_menu ul > li > ul > li').click(function(){
		/*
		var selfClick = $(this).find('ul:first').is(':visible');
		if(!selfClick) {
		  $(this)
			.parent()
			.find('> li ul:visible')
			.slideToggle();
		}
		*/
		
		$(this)
		  .find('ul:first')
		  .stop(true, true)
		  .slideToggle(300);
	  });
	  

	  
	});
//-->
</script>

</head>

<body>
<div id="maincontent_menu">
<ul id="accordion">
            
            <!-- first level -->
        	<li>
        	<a href="page.html" class="active">non expandable</a>
        	</li>
            
            <!-- first level -->
            <li>
        	<a href="page.html">non expandable</a>
        	</li>
            	
            <!-- first level -->
            <li>
        	<a href="page.html">non expandable</a>
        	</li>
            
            <!-- first level -->
            <li>
        	<a href="page.html">non expandable</a>
        	</li>
            
            <!-- first level -->
            <li>
        	<a href="page.html">non expandable</a>
        	</li>
            
            <!-- first level -->
            <li>
        	<a href="page.html">non expandable</a>
        	</li>
            
            <!-- first level -->
            <li class="active">
        	Expandable
        		<ul>
        			<li class="second">
        				Expandable
        				<ul>
        					<li><a href="pagina.html">non expandable</a></li>
        				</ul>
        			</li>
                    <li class="second">
        				<span class="active">expandable</span>
        				<ul>
        					<li><a href="pagina.html">non expandable</a></li>
        					<li><a href="pagina.html" class="active">non expandable</a></li>
                            <li><a href="pagina.html">non expandable</a></li>
        				</ul>
        			</li>
                    <li><a href="pagina.html">non expandable</a></li>
        		</ul>
        	</li>
            
        </ul> 
        </div>
</body>
</html>

Open in new window

0
Comment
Question by:wal_toor
  • 3
  • 3
7 Comments
 
LVL 7

Expert Comment

by:JosephEricDavis
ID: 34120883
Not sure if this is exactly what you're after, but it sounds to me like you're just trying to add a class to an element when the page loads. Yes?

If so, inside your $(document).ready(function() {} You need to add something like this.

$('#ElementID').addClass('ClassNameToAdd');
$('#ElementID').addClass('ClassNameToAdd');

Open in new window

0
 
LVL 8

Author Comment

by:wal_toor
ID: 34120971
Thanks Joseph

But this was not what i meant. The classnames are already in the example code. I need the LI elements that have this classname to be default expanded.

greetz,
walter
0
 
LVL 16

Expert Comment

by:jmatix
ID: 34121056
If you want the expandable items to be expanded by default, change the <script> as below:
<script type="text/javascript">

<!-- menu

	$(document).ready(function(){

	  $('#maincontent_menu ul > li ul')

		.click(function(e){

		  e.stopPropagation();

		}).attr("class", "active");

		/*.filter(':not(:first)')*/

		.hide();

		

	  $('#maincontent_menu ul > li, #maincontent_menu ul > li > ul > li').click(function(){

		/*

		var selfClick = $(this).find('ul:first').is(':visible');

		if(!selfClick) {

		  $(this)

			.parent()

			.find('> li ul:visible')

			.slideToggle();

		}

		*/

		

		$(this)

		  .find('ul:first')

		  .stop(true, true)

		  .slideToggle(300);

	  });

	  



	  

	});

//-->

</script>

Open in new window

0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 16

Expert Comment

by:jmatix
ID: 34121126
Sorry, made a typo. See corrected below:
<script type="text/javascript">

<!-- menu

	$(document).ready(function(){

	  $('#maincontent_menu ul > li ul')

		.click(function(e){

		  e.stopPropagation();

		}).attr("class", "active")

		/*.filter(':not(:first)')*/

		.show();

		

	  $('#maincontent_menu ul > li, #maincontent_menu ul > li > ul > li').click(function(){

		/*

		var selfClick = $(this).find('ul:first').is(':visible');

		if(!selfClick) {

		  $(this)

			.parent()

			.find('> li ul:visible')

			.slideToggle();

		}

		*/

		

		$(this)

		  .find('ul:first')

		  .stop(true, true)

		  .slideToggle(300);

	  });

	  



	  

	});

//-->

</script>

Open in new window

0
 
LVL 8

Author Comment

by:wal_toor
ID: 34121419
Hellow jmatix,

Thanks for your reply, but not all the expandable items need te be expanded, only the ones that have the class 'active'

greetz,
walter
0
 
LVL 16

Accepted Solution

by:
jmatix earned 500 total points
ID: 34121700
Try this:
<script type="text/javascript">

<!-- menu

	$(document).ready(function(){

	  $('#maincontent_menu ul > li ul')

		.click(function(e){

		  e.stopPropagation();

		})

		/*.filter(':not(:first)')*/

		.hide();

	

	  $('.active').parents().show();



	  $('#maincontent_menu ul > li, #maincontent_menu ul > li > ul > li').click(function(){

		/*

		var selfClick = $(this).find('ul:first').is(':visible');

		if(!selfClick) {

		  $(this)

			.parent()

			.find('> li ul:visible')

			.slideToggle();

		}

		*/

		

		$(this)

		  .find('ul:first')

		  .stop(true, true)

		  .slideToggle(300);

	  });

	  



	  

	});

//-->

</script>

Open in new window

0
 
LVL 8

Author Closing Comment

by:wal_toor
ID: 34134486
Yes, thats it! thanks
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
ust html in ms/access WebBrowswer 11 45
Element alignment and word wrapping 9 28
HTML CSS 7 20
Asp.net mvc foreach 3 12
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
This article discusses four methods for overlaying images in a container on a web page
In this tutorial viewers will learn how add a full-size background image to a webpage using CSS3. Create a new HTML document with an internal stylesheet.: In CSS, define the html element to have a background image. Use a high resolution image.: In t…
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now