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

LVL 7
rgranlundAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MlandaTCommented:
$(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

0
rgranlundAuthor Commented:
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.
0
MlandaTCommented:
I see.
$(function() {

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

        e.preventDefault();
        $(this).closest('ul').find('.sublinks').slideUp();
        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

When I commented out that line 11, it seemed to work better. Also, instead of just .toggle() on line 7, I've changed that to .slideUp()
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rgranlundAuthor Commented:
When I implement that on my page, there only thing it still does is change the plus to minus and nothing opens
0
Julian HansenCommented:
Here is some code that should do what you need
Simple CSS to show + - using css.
CSS
<style type="text/css">
ul {
  list-style: none;
}
ul ul {
  display: none;
}
a.viewmore:before {
  font-family: Arial;
  content: "+";
  font-size: 16px;
  padding-right: 8px;
}
a.viewmore.expanded:before {
  content: "-";
}
a, a:hover {
	text-decoration: none;
}
</style>

Open in new window

HTML
    <ul>
      <li><a href="#" class="viewmore">Menu 1</a>
        <ul class="sublinks">
          <li>Menu 1 Sub link 1</li>
          <li>Menu 1 Sub link 2</li>
          <li>Menu 1 Sub link 3</li>
        </ul>
      </li>
      <li><a href="#" class="viewmore">Menu 2</a>
        <ul class="sublinks">
          <li>Menu 2 Sub link 1</li>
          <li>Menu 2 Sub link 2</li>
          <li>Menu 2 Sub link 3</li>
        </ul>
      </li>
      <li><a href="#" class="viewmore">Menu 3</a>
        <ul class="sublinks">
          <li>Menu 3 Sub link 1</li>
          <li>Menu 3 Sub link 2</li>
          <li>Menu 3 Sub link 3</li>
        </ul>
      </li>
    </ul>

Open in new window

JQuery
$(function() {
  $('a.viewmore').click(function(e) {
    e.preventDefault();
    var vis = $(this).next().is(':visible');
    $('.sublinks').slideUp();
    $('.viewmore').removeClass('expanded');
    if (!vis) {
      $(this).addClass('expanded').next().slideDown();
    }
  });
});

Open in new window

Working sample here
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.