SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast whitecreek's Avatar
    Join Date
    Aug 2011
    Location
    Madrid, Spain
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Swapping up/down arrow icons issue - jquery accordion

    Hi there,

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

    http://jsfiddle.net/cFpc9/5/

    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.

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by whitecreek View Post
    I tried an 'if statement' too without success. I guess it wonīt be such a difficult thing to get!! Any tip?
    Here's an update that does the job:

    Code javascript:
    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:

    Code javascript:
    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.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Enthusiast whitecreek's Avatar
    Join Date
    Aug 2011
    Location
    Madrid, Spain
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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:

    Code JavaScript:
    ? 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 ...

    http://jsfiddle.net/cFpc9/9/

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by whitecreek View Post
    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 ...
    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.

    Code javascript:
    $(this).find('span').replaceWith(indicator.clone());
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Enthusiast whitecreek's Avatar
    Join Date
    Aug 2011
    Location
    Madrid, Spain
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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 .
    .clone definitely sorted that out.

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

    http://jsfiddle.net/cFpc9/10/

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

  6. #6
    SitePoint Enthusiast
    Join Date
    Aug 2008
    Location
    india
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •