jQuery disable button behaviour once selected -
i have 2 buttons side side open separate bits of content below, want button associated visible bit of content nothing once selected again.
at moment when button clicked again, content closes. (basically there needs content showing.)
also, possible have first button content open default?
here code: https://jsfiddle.net/0ptdm8qs/2/
any suggestions appreciated!
$(document).ready(function(){ $('.firstbutton').click(function(){ if($('#first').is(':visible')) { $('#first').hide(); } else { $('#second').hide(); $('#first').fadein(); } }); }); $(document).ready(function(){ $('.secondbutton').click(function() { if ($('#second').is(':visible')) { $('#second').fadeout(); if ($("#second").data('lastclicked') !== this) { $('#second').fadein(); } } else { $('#first').hide(); $('#second').fadein(); } $("#second").data('lastclicked', this); }); }); $(document).ready(function(){ $('.button').click(function() { $('.button').not(this).removeclass('buttonactive'); $(this).toggleclass('buttonactive'); }); });
you can remove toggle code, add .buttonactive class .firstbutton , set display: block; #first.
$(document).ready(function(){ $('.firstbutton').click(function(){ $('#second').hide(); $('#first').fadein(); }); }); $(document).ready(function(){ $('.secondbutton').click(function() { $('#first').hide(); $('#second').fadein(); $("#second").data('lastclicked', this); }); }); $(document).ready(function(){ $('.button').click(function() { $(this).addclass('buttonactive'); $('.button').not(this).removeclass('buttonactive'); }); });
#container { float: left; display: inline; background: #fff; height: auto; } #first { display: block; width: 500px; height: 300px; background-color: #ec644b; color: #fff; font-family: arial; font-size: 30px; text-align: center; } #second { display: none; width: 500px; height: 300px; background-color: #446cb3; color: #fff; font-family: arial; font-size: 30px; text-align: center; } .button { width: 250px; display: inline-block; clear: both; margin: 10px 0; font-size: 24px; font-family: arial; font-weight: 300; line-height: 49px; text-align: center; color: #fff; cursor: pointer; } .firstbutton { background-color: #ec644b; } .secondbutton { background-color: #446cb3; } .buttonactive { color: #000; background-color: #fff; border-bottom: 5px solid #000; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="http://code.jquery.com/jquery-2.0.0b1.js"></script> <div id="container"> <span class="button firstbutton buttonactive">first button</span> <span class="button secondbutton">second button</span> <div id="first">one</div> <div id="second">two</div> </div>
Comments
Post a Comment