Swapping up/down arrow icons issue - jquery accordion

Hi there,

Im working out a jquery based accordion menu. Here’s the thing:

I might as well be leaving it as it is right now, but I’d like the up and down ( - respectively) triangle symbols to swap depending on whether .2ndul is :visible or :hidden. I tried with the commented code in the ‘javascript’ box knowing beforehand it won’t probably work. You got it. It didn’t.

I tried an ‘if statement’ too without success. I guess it won´t be such a difficult thing to get!! Any tip?

Any help will be much appreciated.

Best regards.

Here’s an update that does the job:


var downArrow = $('<span>', {class: 'arrows', html: '&#x25BC;'}),
    upArrow = $('<span>', {class: 'arrows', html: '&#x25B2;'});

$('.2ndul').hide();            
$('.2ndul:hidden').prev('.topic').append(downArrow);

$('.topic').click(function() {                
    var indicator = $(this).find('span');
    
    $('.2ndul:visible').not($(this).next('li')).slideToggle(300);
    $(this).next('.2ndul').slideToggle(300);
    
    indicator = (indicator.html() === downArrow.html()) ? upArrow : downArrow;
    $(this).find('span').replaceWith(indicator);
});

Even better though would be to detect whether the toggled element is visible or not, and to base the indicator on that instead.

Which gives us this adjusted code instead:


var downArrow = $('<span>', {class: 'arrows', html: '&#x25BC;'}),
    upArrow = $('<span>', {class: 'arrows', html: '&#x25B2;'});

$('.2ndul').hide();            
$('.2ndul:hidden').prev('.topic').append(downArrow);

$('.topic').click(function() {                
    var target = $(this).next('.2ndul'),
        indicator = target.is(':visible') ? downArrow : upArrow;
    
    $('.2ndul:visible').not(target).slideToggle(300);
    target.slideToggle(300);
    
    $(this).find('span').replaceWith(indicator);
});

The code is now a lot easier to understand in terms of how the indicator works.

Thanks for answering Paul.

Yeap, I kind of understand second chunk of code a little better. I definitely need to get used to store things in variables. It really clears things up.

My first thinking was detecting whether the toggled element is visible or not. I guess I didn´t make it to work out and keep ‘dinamicity’ at the same time.

I never saw this syntax before:

? downArrow : upArrow;

I take it swapping arrows is what that bit really does. There’s one weird thing happening though. After toggling ‘target’, that is, after clicking on any .topic and showing and hiding .2ndul underneath this .topic, some of the arrow symbols in other .topic disappear. All of them but the last one. At the end there’s only one arrow symbol left :eek:

Ahh, good catch. What’s happening is that replaceWith is not adding a copy of the indicator, but is instead moving a reference to indicator from one place to another. We can fix that issue by replacing the arrow with a copy (a clone) of the indicator instead.


$(this).find('span').replaceWith(indicator.clone());

Got it. Didn´t know about .clone either. Never was in such a situation to need it yet either!! These guys from JQuery have it all covered :D.
.clone definitely sorted that out.

The whole of your chunk put together, in case anyone needs it, here:

Thx a million for your time Paul. Best regards!!!

This script has a problem in ie browsers on page load all the listing display on the screen by default and accordion/toggling doesn’t effect them. is there any possible way to work this script in IE browsers as well.