Link to home
Start Free TrialLog in
Avatar of Robert Granlund
Robert GranlundFlag for United States of America

asked on

jQuery Toggle

I have a piece of code that toggles open parts of <ul> when you click on a PLUS sign.  And then it closes again, when it is clicked.  There are many of these on a large menu.  What I would like to add to the script is that if you click on a PLUS sign and there are other plus sigs that have been toggled open, they will close, so only one one toggled are is open at a time.  The following script opens the ul but then it closes it again right away.  Can anyone help me sort this out?

$(function() {
	 var $sublinks = $('.sublinks');
	$('.viewmore').click(function(e) {
    e.preventDefault();
    $(this).closest('ul').find('.sublinks').toggle();
    var text =  ($(this).text() == '+') ? '-' : '+';
    $(this).html(text);
    var $ans = $(this).next(".sublinks").stop(true).slideToggle(100);
    $sublinks.not($ans).filter(':visible').stop(true).slideUp();
  });


  
});

Open in new window

Avatar of Mlanda T
Mlanda T
Flag of South Africa image

$(function() {

    var $sublinks = $('.sublinks');
    $('.viewmore').click(function(e) {

        e.preventDefault();
        $(this).closest('ul').find('.sublinks').toggle();
        var text = ($(this).text() == '+') ? '-' : '+';
        $(this).html(text);

        $sublinks.filter(':visible').stop(true).slideUp();
        var $ans = $(this).next(".sublinks").stop(true).slideToggle(100);

    });

});

Open in new window

Avatar of Robert Granlund

ASKER

That does the same thing.  It opens it and closes it immediatley: http://cohr-dev-ee01.azurewebsites.net/
Click on Products and then click on the first PLUS sign to see what I mean.
ASKER CERTIFIED SOLUTION
Avatar of Mlanda T
Mlanda T
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
When I implement that on my page, there only thing it still does is change the plus to minus and nothing opens
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial