Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

JavaScript accordion menu

Featured Replies

Trying to learn javascript and have created an accordion menu. When the page is displayed, all the menu items should be collapsed and when clicked on individually should expand/collapse. Subsequent clicks should result in the expanded item collapsing and the clicked item expanding. Sadly, when the page is displayed all the items are expanded except the first one. I think it is just a typo somewhere, but any help would be much appreciated :) Here is the javascript -

 

var Accordion =

{

init: function()

{

var accordions = $(".accordion");

 

for (var i = 0, ii = accordions.length; i < ii; i++)

{

var folds = accordions.childNodes;

for (var j = 0, jj = folds.length; j < jj; j++)

{

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

{

Accordion.collapse(folds[j]);

var foldLinks = folds[j].getElementsByTagName("a");

var foldTitleLink = foldLinks[0];

$(foldTitleLink).bind("click", Accordion.clickListener);

 

for (var k = 1, kk = foldlinks.length; k < kk; k++)

{

$(foldLInks[k]).bind("focus", Accordion.focusListener);

}

}

}

}

},

 

collapse: function(fold)

{

$(fold).removeClass("expanded");

$(fold).addClass("collapsed");

},

 

collapseAll: function(accordion)

{

var folds = accordion.childNodes;

for (var i = 0, ii = folds.length; i < ii; i++)

{

if (folds.nodeType == 1)

{

Accordion.collapse(folds);

}

}

},

 

expand: function(fold)

{

Accordion.collapseAll(fold.parentNode);

$(fold).removeClass("collapsed");

$(fold).addClass("expanded");

},

 

clickListener: function(event)

{

var fold = this.parentNode.parentNode;

if ($(fold).hasClass("collapsed"))

{

Accordion.expand(fold);

}

else

{

Accordion.collapse(fold);

}

event.preventDefault();

},

 

focusListener: function(event)

{

var element = this;

while (element.parentNode)

{

if($(element.parentNode).hasClass("accordion"))

{

Accordion.expand(element);

return;

}

element = element.parentNode;

}

}

};

 

Accordion.init();

  • Author

yes! found the error, knew it would be a typo, but so hard to spot.

 

in the following line... $(foldLInks[k]).bind("focus", Accordion.focusListener);

 

Links is spelt Llnks and should be spelt Links.

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.