?
Solved

jquery accordion state

Posted on 2009-12-18
6
Medium Priority
?
482 Views
Last Modified: 2012-05-08
Dear friends,

i am using jquery accordion and its working fine

what if i select a accordion tab and its content panel will show and if there is a link in content panel which goes to lets say aboutus.aspx from index.aspx

now when i click on the link its goes to aboutus.aspx but on the aboutus.aspx the accordion remain open the accordion content panel which i selected on the index.aspx.

so i just want the accordion state will remain same on the second page.

thanx
0
Comment
Question by:virafh
  • 3
  • 3
6 Comments
 
LVL 19

Expert Comment

by:Albert Van Halen
ID: 26079970
Every time you switch between a section, you could store the number of the section into a cookie.
You can use the change event for that. (http://jqueryui.com/demos/accordion/#event-change)
During creation of the accordion you can read the cookie and set the active section by using the active option. (http://jqueryui.com/demos/accordion/#option-active)
0
 

Author Comment

by:virafh
ID: 26079991
can you explain with simple example

please see the attached code

<script type="text/javascript">
        $(function() {
            $("#accordion").accordion({
            active: false, autoHeight: false
            });
        });
    </script>

Open in new window

0
 
LVL 19

Accepted Solution

by:
Albert Van Halen earned 500 total points
ID: 26080539
Check the code...
The code for getting and setting cookies is taken from quirksmode.org
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js"></script>

<script type="text/javascript">
	function createCookie(name, value, days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
			var expires = "; expires=" + date.toGMTString();
		}
		else var expires = "";
		document.cookie = name + "=" + value + expires + "; path=/";
	}

	function readCookie(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for (var i = 0; i < ca.length; i++) {
			var c = ca[i];
			while (c.charAt(0) == ' ') c = c.substring(1, c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
		}
		return null;
	}

	$(function() {
		var activeSection;
		try {
			activeSection = parseInt(readCookie("ActiveSection"));
		}
		catch (err) {
			activeSection = 1;
		}

		$("#accordion").accordion({
			active: activeSection,
			change: function(event, ui) {
				// Get all previous siblings in order to determine the index
				createCookie("ActiveSection", ui.newHeader.prevAll('h3').length);
			}
		});
	});
</script>

<div id="accordion">
	<h3><a href="#">Section 1</a></h3>
	<div>
		<p>
		Mauris mauris ante, blandit et, ultrices a, suscipit eget, quam. Integer
		ut neque. Vivamus nisi metus, molestie vel, gravida in, condimentum sit
		amet, nunc. Nam a nibh. Donec suscipit eros. Nam mi. Proin viverra leo ut
		odio. Curabitur malesuada. Vestibulum a velit eu ante scelerisque vulputate.
		</p>
	</div>
	<h3><a href="#">Section 2</a></h3>
	<div>
		<p>
		Sed non urna. Donec et ante. Phasellus eu ligula. Vestibulum sit amet
		purus. Vivamus hendrerit, dolor at aliquet laoreet, mauris turpis porttitor
		velit, faucibus interdum tellus libero ac justo. Vivamus non quam. In
		suscipit faucibus urna.
		</p>
	</div>
	<h3><a href="#">Section 3</a></h3>
	<div>
		<p>
		Nam enim risus, molestie et, porta ac, aliquam ac, risus. Quisque lobortis.
		Phasellus pellentesque purus in massa. Aenean in pede. Phasellus ac libero
		ac tellus pellentesque semper. Sed ac felis. Sed commodo, magna quis
		lacinia ornare, quam ante aliquam nisi, eu iaculis leo purus venenatis dui.
		<a href="Accordion.html">Click me...</a>
		</p>
		<ul>
			<li>List item one</li>
			<li>List item two</li>
			<li>List item three</li>
		</ul>
	</div>
	<h3><a href="#">Section 4</a></h3>
	<div>
		<p>
		Cras dictum. Pellentesque habitant morbi tristique senectus et netus
		et malesuada fames ac turpis egestas. Vestibulum ante ipsum primis in
		faucibus orci luctus et ultrices posuere cubilia Curae; Aenean lacinia
		mauris vel est.
		</p>
		<p>
		Suspendisse eu nisl. Nullam ut libero. Integer dignissim consequat lectus.
		Class aptent taciti sociosqu ad litora torquent per conubia nostra, per
		inceptos himenaeos.
		</p>
	</div>
</div>

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:virafh
ID: 26187609
Dear AlbertVanHalen,

please give me sometime, i will check it and update you on this.

thanx
0
 
LVL 19

Expert Comment

by:Albert Van Halen
ID: 26188514
No worries virafh; take your time...
0
 

Author Closing Comment

by:virafh
ID: 31667724
Thank you
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hi all! Recently there was EE question and the person wanted to have a multi-column textbox <div> selection, so as a first step to answer I provided a link but that was not complete with JavaScript selection, but had a good style sheet. So as a ques…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses
Course of the Month14 days, 7 hours left to enroll

839 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