473,394 Members | 1,965 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Creating an Accordion w/ Event Listeners

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 (assuming a file "core.js") :

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:

for (var k = 1; k < foldLinks.length; k++)
{
Core.addEventListener(
foldLinks[k], "focus", Accordion.focusListener);
}

It's supposed to be a script insert for users 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.

--
Message posted via WebmasterKB.com
http://www.webmasterkb.com/Uwe/Forum...cript/200807/1

Jul 19 '08 #1
2 2140
"LayneMitch via WebmasterKB.com" <u39402@uwewrote in message
news:87625c291ea59@uwe...
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
Hi,

Whats the name of the book out of interest ?

Aaron

Jul 25 '08 #2
On Jul 20, 12:27*am, "LayneMitch via WebmasterKB.com" <u39402@uwe>
wrote:
Greetings.

I'm reading this book
Which book?
that is teaching me the more appropriate way of
assigning functions.
More appropriate than what? If you explain the method, you might get
some comment on the scenarios that it is "more appropriate" for.

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.
[...]
>
JavaScript Code (assuming a file "core.js") :
But you haven't given indication of what is in "core.js", at least
include some comment on what functions do or better, post a link to
where the code can be found.

var Accordion =
{
* init: function()
* {
* * var accordions = Core.getElementsByClass("accordion");
Presumably accordions is now an array with references to all elements
with a class of "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)
It may be better for future compatibility to use:

if (folds[j].ELEMENT_NODE == folds[j].nodeType)

* * * * {
* * * * * Accordion.collapse(folds[j]);
* * * * * var foldsLinks = folds[j].getElementsByTagName("a");
* * * * * var foldTitleLink = foldLinks[0];
The code seems to depend on a certain HTML structure, it is better not
to assume that there will be an a element and use:

if (foldTitleLink) {
* * * * * Core.addEventListener(
* * * * * * * foldTitleLink, "click", Accordion.clickListener);
And what does Core.addEventListener do? Presumably it wraps the W3C
EventTarget interface's addEventListener and IE's attachEvent methods.
>
* * * * * for (var k = 1; k < foldLinks.length; k++)
* * * * *{
* * * * * * *Core.addEventListener(
* * * * * * * * foldLinks[k], "focus", Accordion.focusListener);
* * * * * }
Presumably when the a elements get focus, they are made visible on the
page.
[...]
The part that I don't understand is:

*for (var k = 1; k < foldLinks.length; k++)
* * * * *{
* * * * * * *Core.addEventListener(
* * * * * * * * foldLinks[k], "focus", Accordion.focusListener);
* * * * * }

It's supposed to be a script insert for users that don't have a mouse. Soif
they tab to every link, it's still going to tab through the collapsed links
that are pushed off to the left.
The links are made visible by moving them back onto the page. Have
you tried putting the example in a browser and running it?
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.
Apparently you can't, otherwise you wouldn't have asked. :-)

Put the code into a page and run it. I can't say whether it works or
not as I don't have a copy of core.js to use, nor do I know the
structure of the HTML required for it to work and I don't have the
time or inclination to reverse engineer it.
--
Rob
Jul 25 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: abs | last post by:
My element: <span onclick="alert('test')" id="mySpan">test</span> Let's say that I don't know what is in this span's onclick event. Is it possible to add another action to this element's onclick...
0
by: Kamilche | last post by:
''' event.py An event manager using publish/subscribe, and weakrefs. Any function can publish any event without registering it first, and any object can register interest in any event, even...
1
by: Marek Murin | last post by:
Hi all, I have created vb.net user control that has to be used by vb6 form. Everything goes well with putting the vb.net user control on the VB6 form until I want to receive any event from my...
13
by: Tony | last post by:
And that is probably very unclear - so let me explain. Please forgive me if I screw up the terminology (and let me know what the correct term would be): What I want to do is to create a library...
4
by: =?Utf-8?B?R2VyaGFyZA==?= | last post by:
I am just stating to use ajax, and have a perfect place to use the accordion control. I have it working fine, but need to have a normal asp button in one of the panes that fires an onclick event...
3
by: srilakshmim | last post by:
Hello I want to create Accordion (Ajax Control Tool Kit)dynamically. My Code is as follows Code: ( cpp ) protected void Page_Load(object sender, EventArgs e) { try ...
1
by: John Graham | last post by:
I have a form where I separate several of the sections with the use of an accordion. Because of the requirements I have, I need the form to be saved every time the user clicks on a different pane...
1
by: petersonus115 | last post by:
Hi my name is jewel.. i am having a problem with accordion pane.. I am using newest version of ajax control tool kit.. i have two text box and one button in my accordion pane... I want to execute...
3
by: Allen Chen [MSFT] | last post by:
Hi Richard, Quote from Richard================================================== However I also want to be able to remove the panes. I have tried to include this, but find that when I first...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.