views:

153

answers:

3

Hi,

I would like to create my own accordion component without using any AJAX toolkits, mostly for learning purposes. I am not sure quite where to start with this one. I'm assuming I would begin by creating div's for each section in the accordion. Perhaps each div would contain a header, which would be the actual button selected to move the accordion to that section. I am not sure the correct approach to take once an accordion's section button is selected though. Would I use the z-order, so that each section is of a higher z-order? Any help is appreciated.

Thanks

A: 

See this question, you will notice my answer contains a demo with the basic workings that should get you started. It was only asked a few minutes ago!

It uses jQuery.

redsquare
He said he doesn't want to use any toolkit
Clement Herreman
He tagged the question with jquery and it shows the markup
redsquare
I assumed he meant he did not want to use the jquery-ui accordian widget
redsquare
Anyway jQuery is not an 'AJAX tookit'
redsquare
+1  A: 

The best way to order it would be like this

<div id="accordion">
 <h3 class="accordion title">Title</h3>
 <div class="accordion section">
  Section Content
 </div>

 <h3 class="accordion title">Title 2</h3>
 <div class="accordion section">
  Section Content
 </div>

 <h3 class="accordion title">Title 3</h3>
 <div class="accordion section">
  Section Content
 </div>

 <h3 class="accordion title">Title 4</h3>
 <div class="accordion section">
  Section Content
 </div>
</div>

You would want to avoid z-order entirely because it is a compatibility mess. Instead you would have the accordion titles be what you would click to open the accordion. You would want to set all of the accordion section <div>'s to visibility:hidden; by default, and then, when one of them is clicked, change it's visibility, and hide all the others. If you want it to work with any amount of accordion sections, you would have it count each <h3 class="accordion title"> and each <div class="accordion section">, and pair those up into an array. When a title is clicked, show it's corresponding div. Alternatively you could give each one a separate ID, but the first way would be much more useful.

Actually, it might be display:none; instead of visibility:hidden;, I would try both.

In addition it's worth mentioning that the animation is usually handled by changing things like the size of the div, so if you were hiding a section, you would make the height smaller and smaller until it reaches 0 and is hidden.

Sneakyness
IDs must be unique: you can't have several elements with the same ID. Also, the value of the ID attribute can't contain spaces. See HTML 4.01: http://www.w3.org/TR/html401/struct/global.html#adef-id and http://www.w3.org/TR/html401/types.html#type-name
NickFitz
My bad, I meant to use class for like half of those! Thanks for catching that. :)
Sneakyness
No problem - I had a feeling you might have meant "class" :-)
NickFitz
+1  A: 

I would highly recommend picking up a book such as John Resig's Pro JavaScript techniques that will give you some ideas and initial thoughts about how to approach bulding your own client-side solutions.

Essentially, you would have an element to act as a header, for example <h1> or <div> under which you would have a <div> with an initial style of display: none;. Set up an event handler on the click event of the header to change the style of the div below to display: block and ensuring that any other content <div>s are hidden (do this by using a CSS class on each content <div> for example).

I'll leave the smooth animation to you as an exercise for how it might be accomplished. As a hint, I would recommend looking at how a JavaScript library like jQuery handles animation, by checking out the source.

Russ Cam