Jump to content

Jquery toggle issue

Go to solution Solved by thisperson,

I tried this in a JS fiddle and it didn't work. Not sure why.

Sorry was my fault, forgot the var's.

 

http://codepen.io/thisperson/pen/QbqjvZ

I have a toggle which starts out with a id element being shown as hidden and when a onclick event occurs it switches back to show. The problem is it creates an additive effect. For example, if I have two toggles like the below when I click on both of them they both show up at the same time. I'd like it when I click on another button the contents of the div (in this case 'add' below) should disappear.

<script>		$(document).ready(function(){		$("#add").hide();		  $("#addanime").click(function(){			$("#add").toggle();		  });		});</script>		<script>		$(document).ready(function(){		$("#all").hide();		  $("#viewall").click(function(){			$("#all").toggle();		  });		});</script></head><body><h1>Button Togglers</h1><button id='addanime'>Button 1</button><br /><button id='viewall'>Button 2</button><div id='add'><p>This is some text</p></div><div id='all'><p>This is also some text</p></div>
Link to comment
https://linustechtips.com/topic/387728-jquery-toggle-issue/
Share on other sites

Link to post
Share on other sites

 

You can optimise your code a lot more:

<script>$(document).ready(function(){   $("#add,#all").hide();   $("#addanime").click(function(){      $("#add").toggle();   });      $("#viewall").click(function(){      $("#all").toggle();   });});</script>

 

Thanks, still need a way to hide the other div when the second buttons clicked.

Link to comment
https://linustechtips.com/topic/387728-jquery-toggle-issue/#findComment-5238801
Share on other sites

Link to post
Share on other sites

<script>add = false;all = false;$(document).ready(function(){   $("#add,#all").hide();   $("#addanime").click(function(){      $("#add").toggle();      add = true;      if($all === true) {        $("#all").toggle();        all = false;      } else {              }   });   $("#viewall").click(function(){      $("#all").toggle();      all = true;      if($add === true) {        $("#add").toggle();        add = false;      } else {              }   });});</script>

Hopefully this should solve it for you.

Link to comment
https://linustechtips.com/topic/387728-jquery-toggle-issue/#findComment-5241071
Share on other sites

Link to post
Share on other sites

<script>add = false;all = false;$(document).ready(function(){   $("#add,#all").hide();   $("#addanime").click(function(){      $("#add").toggle();      add = true;      if($all === true) {        $("#all").toggle();        all = false;      } else {              }   });   $("#viewall").click(function(){      $("#all").toggle();      all = true;      if($add === true) {        $("#add").toggle();        add = false;      } else {              }   });});</script>

Hopefully this should solve it for you.

 

 

I tried this in a JS fiddle and it didn't work. Not sure why.

Link to comment
https://linustechtips.com/topic/387728-jquery-toggle-issue/#findComment-5248166
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×