Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Accordion Help

Status
Not open for further replies.

DotNetGnat

Programmer
Mar 10, 2005
5,548
IN
Guys,

I am using the following code that I found online for creating an accordion. It works fine but I want a way to navigate from tab 1 to tab 2 and from tab2 to tab3. I mean on tab1, I want to have a link/button/anchor that when clicked activates and opens tab2 and similarly on tab 2 I want a link that activates tab3. The actual reason for this is: I have a form on tab 1 that I need to validate and then only proceed the user to tab 2. My validate function returns true or false. If it is true I want to activate tab 2.

Below is the code in the simplest format.
Code:
HTML
______
<html>
<head>
<link rel="stylesheet" href="style.css" type="text/css" />
<script type="text/javascript" src="script.js"></script>
</head>
<body>
<div id="accordion">
	<dl class="accordion" id="slider">
		<dt>Tab 1</dt>
		<dd>
	<span>
				Content Tab 1
				</span>
		</dd>
		<dt>Tab 2</dt>
		<dd>
			<span>
			Content - Tab 2
			</span>
		</dd>
		<dt>Tab 3</dt>
		<dd>
			<span>
			Content Tab 3
			</span>
		</dd>
	</dl>
</div>

<script type="text/javascript">

var slider1=new accordion.slider("slider1");
slider1.init("slider",0, "open");

</script>

</body>
</html>

Code:
JS
____
var accordion=function(){
	var tm=sp=10;
	function slider(n){this.nm=n; this.arr=[]}
	slider.prototype.init=function(t,c,k){
		var a,h,s,l,i; a=document.getElementById(t); this.sl=k?k:'';
		h=a.getElementsByTagName('dt'); s=a.getElementsByTagName('dd'); this.l=h.length;
		for(i=0;i<this.l;i++){var d=h[i]; this.arr[i]=d; d.onclick=new Function(this.nm+'.pro(this)'); if(c==i){d.className=this.sl}}
		l=s.length;
		for(i=0;i<l;i++){var d=s[i]; d.mh=d.offsetHeight; if(c!=i){d.style.height=0; d.style.display='none'}}
	}
	slider.prototype.pro=function(d){
		for(var i=0;i<this.l;i++){
			var h=this.arr[i], s=h.nextSibling; s=s.nodeType!=1?s.nextSibling:s; clearInterval(s.tm);
			if(h==d&&s.style.display=='none'){s.style.display=''; su(s,1); h.className=this.sl}
			else if(s.style.display==''){su(s,-1); h.className=''}
		}
	}
	function su(c,f){c.tm=setInterval(function(){sl(c,f)},tm)}
	function sl(c,f){
		var h=c.offsetHeight, m=c.mh, d=f==1?m-h:h; c.style.height=h+(Math.ceil(d/sp)*f)+'px';
		c.style.opacity=h/m; c.style.filter='alpha(opacity='+h*100/m+')';
		if(f==1&&h>=m){clearInterval(c.tm)}else if(f!=1&&h==1){c.style.display='none'; clearInterval(c.tm)}
	}
	return{slider:slider}
}();

Code:
css
____

* {margin:0; padding:0}

#accordion {width:459px; margin:50px auto}
.accordion {width:459px; font:12px Verdana,Arial; color:#033}
.accordion dt {width:439px; border:2px solid #9ac1c9; padding:8px; font-weight:bold; margin-top:5px; cursor:pointer; background:url(images/header.gif)}
.accordion dt:hover {background:url(images/header_over.gif)}
.accordion dd {overflow:hidden; background:#fff}
.accordion span {display:block; width:425px; border:2px solid #9ac1c9; border-top:none; padding:15px}

thanks in advance

-DNG
 
From what I can see, you'd need to call:

Code:
slider1.pro(dtObj)

where 'dtObj' is a pointer to the DT element that you want to show.

Hope this helps,
Dan




Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Thank You Dan. Yes I figured that function pro is taking care of the click events...instead of adding a new link I tried to modify the pro function itself and looks like it is working...the below is what I tried as a test case scenario showing only the function pro:

Code:
         slider.prototype.pro=function(d){
        for(var i=0;i<this.l;i++){
            var h=this.arr[i], s=h.nextSibling; s=s.nodeType!=1?s.nextSibling:s; clearInterval(s.tm);
            if(h==d&&s.style.display=='none'){
                if(i==1)[red]//tab2[/red]
                {
                    if([red]validate()[/red]){
                        s.style.display=''; su(s,1); h.className=this.sl
                        }
                }
                else
                {
                    s.style.display=''; su(s,1); h.className=this.sl
                }
            }
            else if(s.style.display==''){
                su(s,-1); h.className=''
                }
        }
    }

as you can see I want to open tab 2 only when the validate function returns true...and it works just fine. But when the validate function returns false...it works too and shows error messages but the tab 1 span is closed...
I want tab 1 span to be open so that the user can see the error messages, correct them and move to tab 2.

any suggestions...

-DNG
 
I fixed it guys...thanks for your comments...

-DNG
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top