Domanda

I have a program that is configured to hide/show table rows when a +/- icon is clicked. The functionality is working, however, I need to figure out a way to reset all hide/show icons when the parent category is toggled closed.

$(function(){
//src vars
var hide_src = "http://www.synchronizeddesigns.com/filter_hide.gif",
    reveal_src = "http://www.synchronizeddesigns.com/filter_reveal.gif",
        s = '';
//hide all sublevel elements
$(".subsub, .subsubsub").hide();

$("a").click(function(e){
    e.preventDefault();
    var tID = e.target.id,
        tClass = '.' + tID.replace('HS', '');

    $(tClass).toggle();

    if(!$(tClass).is(':visible')){
        s = hide_src;

        //for each subcategory
        $(tClass).each(function(){
            //get class names into classes array
            var classes = $(this).attr('class').split(' '),
                parentClass = '';

            //search classes array for class that begins with 'cat'
            for (var j=0; j<classes.length; j++) {
                if (classes[j].match("cat")){ 
                    parentClass = classes[j];
                }
            }

            //find subsubsub elements that have a class that begins with 'cat#sub'
            var subs = $('[class*=' + parentClass + 'sub]');

            //if there are sub elements, hide them too
            if(subs){
                subs.hide();

                /*****************************************************
                NEED HELP HERE !!!!!!!!!!
                Need a way to reset all hide/show images icon
                when 'parentClass' hide/show is clicked to close.
                *****************************************************/

            }
        });
    } else {
        s = reveal_src;
    }
    //Change image src
    $("#" + tID).attr('src', s);
});
});

To replicate: Toggle open all parents and subs, then close one of the parents, then reopen the parent. You'll notice that the +/- icon remains in it's previous state

jsFiddle link

È stato utile?

Soluzione

You can find the img nodes for the subcategories under the current one and then change their src attr.

I've updated your jsfiddle: http://jsfiddle.net/nTyWv/12/

The code could be something like:

            if(subs){
                innerIcons = jQuery.find("a > img[id*="+tID+"sub]");
                if (innerIcons) {
                    $(innerIcons).attr('src', s);
                };
                subs.hide();
            }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top