Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Creating an Accordion w/ Event Listeners
Old 07-19-2008, 11:23 AM Creating an Accordion w/ Event Listeners
Skilled Talker

Posts: 70
Location: Atlanta, GA
Trades: 0
Greetings.

I'm reading this book that is teaching me the more appropriate way of assigning functions. It has an 'accordian' example that when you click on a link, it expands into other options and when you click it again it 'collapses'.

I understand the most of it, but there is just one part I don't understand.
Here's the code.

Assuming this small HTML/CSS file (code cut short/suppose to collapse links by moving to the left):

ul.accordion li.collapsed * {
position:absolute;
left: -10000px;
}

ul.accordion li.collapsed h2, ul.accordion li.expanded h2,
ul.accordion li.collapsed h2 a:link,
ul.accordion li.collapsed h2 a:visited,
ul.accordion li.expanded h2 a:link,
ul.accordion li.expanded h2 a:visited {
position:static;
}

JavaScript Code:

var Accordion =
{
init: function()
{
var accordions = Core.getElementsByClass("accordion");

for (var i = 0; i < accordions.length; i++)
{
var folds = accordions[i].childNodes;
for (var j = 0; j < folds.length; j++)
{
if (folds[j].nodeType == 1)
{
Accordion.collapse(folds[j]);
var foldsLinks = folds[j].getElementsByTagName("a");
var foldTitleLink = foldLinks[0];
Core.addEventListener(
foldTitleLink, "click", Accordion.clickListener);

for (var k = 1; k < foldLinks.length; k++)
{
Core.addEventListener(
foldLinks[k], "focus", Accordion.focusListener);
}
}
}
if (location.hash.length > 1)
{
var activeFold = document.getElementById(
location.hash.substring(1));
if (activeFold && activeFold.parentNode == accordions[i])
{
Accordion.expand(activeFold);
}
}
}
},

collapse: function(fold)
{
Core.removeClass(fold, "expanded");
Core.addClass(fold, "collapsed");
},

collapseAll: function(accordion)
{
var folds = accordion.childNodes;
for (var i = 0; i < folds.length; i++)
{
if (folds[i].nodeType == 1)
{
Accordion.collapse(folds[i]);
}
}
},

expand: function(fold)
{
Accordion.collapseAll (fold.parentNode);
Core.removeClass(fold, "collapsed");
Core.addClass (fold, "expanded);
},

clickListener: function(event)
{
var fold = this.parentNode.parentNode;
if (Core.hasClass(fold, "collapsed"))
{
Accordion.expand(fold);
}
else
{
Accordion.collapse(fold)
}
Core.preventDefault(event)
},

focusListener: function(event)
{
var element = this;
while (element.parentNode)
{
if (element.parentNode.className == "accordion")
{
Accordion.expand(element);
return;
}
element = element.parentNode;
}
}
};

Core.start(Accordion);

The part that I don't understand is highlighted in blue. It's supposed to be a script insert for browsers that don't have a mouse. So if they tab to every link, it's still going to tab through the collapsed links that are pushed off to the left. So the book create this code in blue so that it does something when the user tabs through the collapsed links. I really would need more of a visual picture of how this works than anything, because the code I can follow.

Please help. Thanks.
LayneMitch is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 07-19-2008, 01:41 PM Re: Creating an Accordion w/ Event Listeners
Extreme Talker

Posts: 238
Location: United States
Trades: 0
The first part of the code adds an event listener for focus to every link so that when a link gains focus, it calls the focusListener function.

The focusListener function expands the accordion link.

You can focus with either the mouse or the keyboard, so the code in blue seems that it would be used both for the mouse and for the keyboard.

Which book are you reading?
__________________
The interlocking pieces of web development: usability, performance, accessibility, and standards.
frost is offline
Reply With Quote
View Public Profile
 
Old 07-19-2008, 03:15 PM Re: Creating an Accordion w/ Event Listeners
VirtuosiMedia's Avatar
Web Design Made Simple

Posts: 1,228
Trades: 0
If you want another easy way to make an accordion, check out MooTools: http://demos.mootools.net/Accordion
__________________
Want new web resources every day? - Follow me on
Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
VirtuosiMedia is offline
Reply With Quote
View Public Profile Visit VirtuosiMedia's homepage!
 
Old 07-19-2008, 10:49 PM Re: Creating an Accordion w/ Event Listeners
Skilled Talker

Posts: 70
Location: Atlanta, GA
Trades: 0
Frost,

I'm reading "Simply JavaScript" by Kevin Yank & Cameron Adams. Published by Sitepoint.

As far as your explanation, I understand that it adds a focusListener function to every link. But what's throwing me off is the conditional statement:

for (var k = 1; k < foldLinks.length; k++);

The Accordion.clickListener is added to every link with the conditional statment:

for (var j = 0; j < folds.length; j++), which starts with the first <li> in the code. But the above starts with the second anchor tag in the second <li> ( by using var k = 1 ). The book was saying that it inserted the focusListener function because the users with only keyboards to navigate can't avoid navigating through the collapsed lists because they aren't deleted, instead they are pushed off to the side. So as the book states: why not make something happen when the user focuses on those lists. Thus, was the inserted focusListener.
LayneMitch is offline
Reply With Quote
View Public Profile
 
Old 07-24-2008, 01:05 AM Re: Creating an Accordion w/ Event Listeners
Extreme Talker

Posts: 238
Location: United States
Trades: 0
Code:
var foldsLinks = folds[j].getElementsByTagName("a");
var foldTitleLink = foldLinks[0];
Core.addEventListener(foldTitleLink, "click", Accordion.clickListener);
Per the code above, the first anchor element is given a click event.

Code:
for (var k = 1; k < foldLinks.length; k++)
{
    Core.addEventListener(foldLinks[k], "focus", Accordion.focusListener);
}
Next, all of the following anchor elements (starting with the second anchor) are given the focus event. I take back what I said earlier about the focus being used for both mouse and keyword access. While clicking can cause a focus event if it's on the right element, in this particular script, the focus event is only used for the purpose of tabbing or arrow keys (like you said).

I could try to explain how the code works in a visual picture, but it would take an awful lot of words. You have the code though, and the nice thing about JavaScript is that you can test it out right there on your desktop with a text editor and a web browser. Plus, if you use the Firefox browser with the Firebug add-on, it's actually fun to test JavaScript.
__________________
The interlocking pieces of web development: usability, performance, accessibility, and standards.
frost is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Creating an Accordion w/ Event Listeners
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.29835 seconds with 12 queries