Create Accordion Menu Using JQuery

When I had the job interview last weekend, I was asked if I had experience with Accordion Menus. Accordio-what??? I thought it was some kind of Javascript framework for easily creating menus. I was only able to know what it was about until it was workday again and I got to surf for it so I could check what it was exactly. So, an accordion menu is actually a type of a menu that expands content (can be sub menu items or text or image or whatever).

The sample menu that was given to me to modify came from this site. The accordion menu in that URL was created using JQuery and is limited to one level only. There are no sub-menus. I think I did badly in that exam since I have never used JQuery extensively unless they were show() hide() funtionalities. I decided to check JQuery out and try to make it work. What I did was use the same CSS class attributes and changed the code to cater sub-menus no matter how many level(s) a menu item has. The finished code would create an accordian menu that looks like the image on the right. I highlighted the sub-menus as blue and red. I am not much of a CSS stylist so it is up to you to change the existing CSS code to, let’s say, have the text move a bit to the right so that they all won’t be aligned vertically.

Here is the JQuery code

$(document).ready(function() {
	var clicked_menu_head;
	var speed = 300;
 
	$('.menu_head').click(function() {
		clicked_menu_head = $(this);
		if (clicked_menu_head.next().is(':visible')) {
			clicked_menu_head.next().slideUp();
			clicked_menu_head.css({backgroundImage:"url(left.png)"});
		}
 
		// if clicked menu head is submenu, check if there are any submenus in it, then hide
		clicked_menu_head.next().find('p.menu_head').each(function () {
			$(this).next().slideUp(speed);
			$(this).css({backgroundImage:"url(left.png)"});
		});
 
		parent_menu_head = clicked_menu_head.parents('div.menu_body').prev(clicked_menu_head.parents('div.menu_body').size()-1);
 
		if (clicked_menu_head.next('div.menu_body').is(':visible')) {
		} else clicked_menu_head.css({backgroundImage:"url(down.png)"}).next('div.menu_body').slideDown(speed);
 
 
		$('#firstpane').children('p').siblings('p').each(function() {
			// loop through each main menu, if not equal, hide all sub menu
			if (parent_menu_head.attr('id')) {
				if (parent_menu_head.attr('id') != $(this).attr('id')) {
					$(this).children('div.menu_body').slideUp(speed);
				}
			} else {
				if (clicked_menu_head.attr('id') != $(this).attr('id')) {
					$(this).css({backgroundImage:"url(left.png)"})
					$(this).next().slideUp(speed).find('p.menu_head').each(function() {
						$(this).next().slideUp(speed);
					});
				}
			}
 
		});
	});
});

And this is the HTML code. I provided 1 menu item sample that has 2 sub-menu levels.

<body>
<div style="float:left" > <!--This is the first division of left-->
 
	<p><strong> Works on clicking </strong></p>
 
	<div id="firstpane" class="menu_list"> <!--Code for menu starts here-->
		<p id="header1" class="menu_head">Header-1</p>
		<div class="menu_body">
			<a href="#">Link-1</a>
			<a href="#">Link-2</a>
			<a href="#">Link-3</a>	
		</div>
 
		<p id="header2" class="menu_head">Header-2</p>
		<div class="menu_body">
 
			<div class="menu_list">
				<p id="submenu" class="menu_head">Sub-Menu</p>
				<div class="menu_body">
 
					<div class="menu_list">
						<p id="submenu2" class="menu_head">Sub-Menu 2</p>
						<div class="menu_body">
							<a href="#">Sub Link-12</a>
							<a href="#">Sub Link-22</a>
							<a href="#">Sub Link-32</a>	
						</div>
					</div>
 
					<a href="#">Sub Link-2</a>
					<a href="#">Sub Link-3</a>	
				</div>
			</div>
 
			<a href="#">Link-1</a>
			<a href="#">Link-2</a>
			<a href="#">Link-3</a>	
		</div>
 
		<p id="header3" class="menu_head">Header-3</p>
		<div class="menu_body">
			<a href="#">Link-1</a>
			<a href="#">Link-2</a>
			<a href="#">Link-3</a>			
	   </div>
	</div>  <!--Code for menu ends here-->
 
</div>
 
</body>

Notice that in <p> and div tags (with ID firstpane), I added the attribute ID to give them unique identifier names. This is mandatory in my code because I used the tag’s ID to check if the menu header clicked is a parent menu header or a sub menu header.

Make sure that you have a main container, in this case, firstpane with CSS class menu_list. Next in the CSS sequence are the menu_head and the menu_body (contains your menu items or more sub-menus).

Like what you see? Buy me a cup of coffee. Or subscribe to my feeds.


(5 votes, average: 4.80 out of 5)
 Loading ...

Golden Palace Casino

Golden Palace Casino is one of the leading online casinos in the internet today. With a whopping almost 300$ free bonus money, new players will be treated to loads of fun playing their games powered by its software created by one of the reputable companies in the online casino industry today, Playtech. Golden Palace features a variety of table and card games and slot and video poker games. From the traditional 3 reel slots to multi-line slots, and a multitude of poker style games, players get a lot of options which favorite game(s) they wish to play.

It even offers progressive type games for slots, just like in real casinos. With secure connections when connecting and playing the games, you get safe assurance that your account is safe. Are you still new and are afraid to play for real money? Not a problem. The software lets you play for fun without worrying about losing money. This lets you get a feel of what to expect when you decide to play for real. If you are tired of going to having to go to real casinos just to play, sit back, relax and play online casino here at the comfort of your own home.

Like what you see? Buy me a cup of coffee. Or subscribe to my feeds.


(1 votes, average: 5.00 out of 5)
 Loading ...

Cookies In Javascript

Last weekend, I had a job interview with regards to Javascript. I was asked if Javascript can pass values from one page to another. All I said was if it is possible, I can look it up in Google. When my initial exam started, I looked it up if it is indeed possible. And voila, cookies were mentioned. I have never used cookies in Javascript before so I totally had no idea until I looked it up. The best functions for me in setting and retrieving cookies in Javascript are the functions found in a W3Schools column.

Using these two functions below, you can set and get a cookie (depending on the expiry value you specify).

function setCookie(c_name, value, expiredays) {
	var exdate = new Date();
	exdate.setDate(exdate.getDate() + expiredays);
	document.cookie = c_name + "=" + escape(value) + ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}
 
function getCookie(c_name) {
	if (document.cookie.length > 0) {
		c_start = document.cookie.indexOf(c_name + "=");
		if (c_start != -1) { 
			c_start=c_start + c_name.length+1; 
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end == -1) c_end = document.cookie.length;
			return unescape(document.cookie.substring(c_start, c_end));
		} 
	}
	return "";
}

Like what you see? Buy me a cup of coffee. Or subscribe to my feeds.


(No Ratings Yet)
 Loading ...

Joker Fatality Censored In The U.S.

Even before this was released and only a teaser video was shown to the public, I was already excited about this game. This was unexpected for me, Mortal Kombat and … uh … DC? But hey, it is here! And I look forward to checking it out once it hits our malls here. Though I am not the gamer type anymore. I still enjoy watching the game fights when players play the game and the intro video that comes with the game. When I read a news article about the Joker’s fatality being censored in the U.S., I wondered, could the same happen here in the Philippines? As much as I do not like violence, I believe fatalities are what makes Mortal Kombat, well, Mortal Kombat. Without them, it would just be another fighting arcade game. Fatalities are what sets Mortal Kombat from other fighting games on uniqueness. And it has been said that the Joker’s fatality is among the best. Do you like it if the Mortal Kombat VS DC Universe game gets censored at your place? Check out this screenshot of the Joker.

Like what you see? Buy me a cup of coffee. Or subscribe to my feeds.


(No Ratings Yet)
 Loading ...