Link to home
Start Free TrialLog in
Avatar of Nathan Riley
Nathan RileyFlag for United States of America

asked on

Optimize jQuery

Is there a better way to write the below so I don't need 2 on click commands?

$("#lockSelection").click(function() {

        $("#lockSelection").toggle();
        $("#unlockSelection").toggle();

    })
    $("#unlockSelection").click(function() {

        $("#lockSelection").toggle();
        $("#unlockSelection").toggle();

    })

Open in new window

Avatar of Thomas Wheeler
Thomas Wheeler

This is strange you should not need it set up like that. Why is there two different id's here can you elaborate on what you are doing.
ASKER CERTIFIED SOLUTION
Avatar of Alexandre Simões
Alexandre Simões
Flag of Switzerland 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
Avatar of Nathan Riley

ASKER

Here is the HTML, hope that helps.

 <div id="lockSelection" class="text-center" style="margin-top:15px;cursor:pointer;"><i class="fa fa-lock"></i> Lock Selection</div>
                            <div id="unlockSelection" class="text-center" style="margin-top:15px;cursor:pointer;display:none;"><i class="fa fa-unlock"></i> Unlock Selection</div>

Open in new window

Ok, so my solution might be the best option.

You can even use the same event handler if you want to do something else other that just show/hide the div's.
Do this by using the :visible selector to evaluate which is actually visible after the toggles.