View Full Version : Accordion Form: Open on tab

05-19-2010, 01:51 PM
1) Script Title: Accordion

2) Script URL (on DD): http://www.dynamicdrive.com/dynamicindex17/ddaccordionmenu.htm

3) Describe problem: We use Accordion to manage a menu on our site and I decided that it would help make a very large form more manageable. It works brilliantly with the exception of one minor issue: When you are tabbing through the inputs, when you get to the next menu-item which is hidden, the cursor moves back to the first input. I would like to open the next menu-item and put focus in the first field there and hide the previous menu-item.

a) is this possible with Accordion and
b) if not, is there an alternative or could it be coded in to Accordion?


05-19-2010, 09:27 PM
You can use the script's onopenclose event handler (http://www.dynamicdrive.com/dynamicindex17/ddaccordion_suppliment2.htm) to focus a particular form element whenever a header is expanded. Take a look at the below example:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>

<script type="text/javascript" src="ddaccordion.js">

* Accordion Content script- (c) Dynamic Drive DHTML code library (www.dynamicdrive.com)
* Visit http://www.dynamicDrive.com for hundreds of DHTML scripts
* This notice must stay intact for legal use


<style type="text/css">

.mypets{ /*header of 1st demo*/
cursor: hand;
cursor: pointer;
padding: 2px 5px;
border: 1px solid gray;
background: #E1E1E1;

.openpet{ /*class added to contents of 1st demo when they are open*/
background: yellow;

.technology{ /*header of 2nd demo*/
cursor: hand;
cursor: pointer;
font: bold 14px Verdana;
margin: 10px 0;

.openlanguage{ /*class added to contents of 2nd demo when they are open*/
color: green;

.closedlanguage{ /*class added to contents of 2nd demo when they are closed*/
color: red;


<script type="text/javascript">

var elementids=["dog", "cat", "rabbit"]

//Initialize first demo:
headerclass: "mypets", //Shared CSS class name of headers group
contentclass: "thepet", //Shared CSS class name of contents group
revealtype: "mouseover", //Reveal content when user clicks or onmouseover the header? Valid value: "click", "clickgo", or "mouseover"
mouseoverdelay: 200, //if revealtype="mouseover", set delay in milliseconds before header expands onMouseover
collapseprev: true, //Collapse previous content (so only one open at any time)? true/false
defaultexpanded: [0], //index of content(s) open by default [index1, index2, etc]. [] denotes no content.
onemustopen: false, //Specify whether at least one header should be open always (so never all headers closed)
animatedefault: false, //Should contents open by default be animated into view?
persiststate: true, //persist state of opened contents within browser session?
toggleclass: ["", "openpet"], //Two CSS classes to be applied to the header when it's collapsed and expanded, respectively ["class1", "class2"]
togglehtml: ["none", "", ""], //Additional HTML added to the header when it's collapsed and expanded, respectively ["position", "html1", "html2"] (see docs)
animatespeed: "fast", //speed of animation: integer in milliseconds (ie: 200), or keywords "fast", "normal", or "slow"
oninit:function(expandedindices){ //custom code to run when headers have initalized
//do nothing
onopenclose:function(header, index, state, isuseractivated){ //custom code to run whenever a header is opened or closed
if (state=="block") //if header is open




<h2>Example 1:</h2>
<a href="#" onClick="ddaccordion.expandone('mypets', 0); return false">Expand 1st header</a> | <a href="#" onClick="ddaccordion.collapseone('mypets', 0); return false">Collapse 1st header</a> | <a href="#" onClick="ddaccordion.toggleone('mypets', 1); return false">Toggle 2nd header</a>

<h3 class="mypets">Dogs</h3>
<div class="thepet">
Dog: <input type="text" id="dog" />

<h3 class="mypets">Cats</h3>
<div class="thepet">
Cat: <input type="text" id="cat" />

<h3 class="mypets">Rabbits</h3>
<div class="thepet">
Rabbit: <input type="text" id="rabbit" />


Take note of the parts in red. Basically each of the form element is given a unique ID, with all the IDs then stored inside the JavaScript array "elementids". When the 2nd header is expanded for example, the script focuses the 2nd form element within array "elementids", or the form element "cat". The order of the form element IDs inside elementids should correspond to the order of the header the element is part of. Since "dog" is inside the first header, it should be defined first inside array elementids.