jQuery Toggle

I have a script that toggles open classes in a menu if a Plus Sign is Clicked.  The toggels open up additional menu choices of a class.

I have two functions and they are as follows.
$(function() {
  $('.bend-insensitive-fiber').click(function(e) {
    e.preventDefault();
    $(this).closest('ul').find('.sublinks').toggle();
    var text =  ($(this).text() == '+') ? '-' : '+';
    $(this).html(text);
  });

$(function() {
  $('.polarization-maintaining-fiber').click(function(e) {
    e.preventDefault();
    $(this).closest('ul').find('.sublinks').toggle();
    var text =  ($(this).text() == '+') ? '-' : '+';
    $(this).html(text);
  });

Open in new window


The issue I need to solve is this;
If "polarization-maintaining-fiber"  has been toggled open and I click on the Plus Sign for "bend-insensitive-fiber" I need to close the "polarization-maintaining-fiber" toggle and Vise Versa.
LVL 8
rgranlundAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

RobOwner (Aidellio)Commented:
i would wrap them in their own functions so that you can call them anywhere:

$(function() {
  $('.bend-insensitive-fiber').click(function(e) {
    e.preventDefault();
    closeItem('.polarization-maintaining-fiber');
    toggleItem('.bend-insensitive-fiber');
  });

  $('.polarization-maintaining-fiber').click(function(e) {
    e.preventDefault();
    closeItem('.bend-insensitive-fiber');
    toggleItem('.polarization-maintaining-fiber');
  });
});

function toggleItem(selector) {
    $(selector).closest('ul').find('.sublinks').toggle();
    var text =  ($(selector).text() == '+') ? '-' : '+';
    $(selector).html(text);
}

function closeItem(selector) {
    $(selector).closest('ul').find('.sublinks').hide();
    var text =  '-';
    $(selector).html(text);
}

Open in new window


What you've got here is a state machine.  This is where I would utilise a library like knockoutjs to manage that for you and automatically open and close the lists depending on the logic you set.
I outline the basics with knockout here: http://www.experts-exchange.com/articles/18468/Introducing-the-Knockout-JavaScript-Framework-MVVM.html

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
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.