-1

sample

    var main = function() {
    $('.article').click(function() {
        if($(this).hasClass('current')) {
                $(this).children('.description').toggle("slow");
        }
        else {
                $('.article').removeClass('current');
                $('.description').hide();
                $(this).addClass('current');
                $(this).children('.description').toggle("slow");
        };
 });
}
$(document).ready(main);

Hello, I would like to know how to learn how to do a few things with my little on-click toggle list:

Im having a hard time on how to make this toggle list to remain persistent after a user clicks on an already opened article. Right now the way I have it setup the current article will toggle whenever clicked on, I would like it to remain open and to only close when clicked on the heading itself (opposed to anywhere in the item).

Also
+ How to create an open/close all toggle link
+ continuity visual cues that shows list is expandable and if it is either open or closed that will fit this style

b__
  • 29
  • 9

3 Answers3

1

If you do not want a click on a child item to cause closure of your main heading, you need to stop the event propagation.

Please check for full syntax/usage: How to stop event bubbling on checkbox click

Usage:

event.stopPropagation()
Community
  • 1
  • 1
1

You need to target the .article .item and not the .article

var main = function() {
    $('.article .item').click(function() {
        var article = $(this).closest('.article');
        if(article.hasClass('current')) {
                article.children('.description').toggle("slow");
        }
        else {
                $('.article').removeClass('current');
                $('.description').hide();
                article.addClass('current');
                article.children('.description').toggle("slow");
        };
   });
}

Demo at http://jsfiddle.net/kL70z8ky/5/

Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317
1

Here a nother aproach:

$('.article').click(function () {
    $(".article").not($(this).addClass('current')).removeClass('current');
    $('.description').not($('.description',this).slideDown()).slideUp();
});
Steven Web
  • 1,966
  • 13
  • 23