jquery - Show/hide divs on click, but prevent the script from executing if same link is clicked a second time -
the script works fine when clicking on of links. want prevent show/hide script executing if same link clicked second time. have found few things on site , others try .unbind being promising, function starts $(this).click(function (), , unbinding kills everything. cannot figure out way restructure code checks togglediv before click against togglediv after click see if same , removes scripts.
codepen: http://codepen.io/jpscoggan/pen/gchbk
here js code:
//show hide (function ($) { $.fn.showhide = function (options) { //default vars plugin var defaults = { speed: 200, easing: 'swing' }; var options = $.extend(defaults, options); $(this).click(function () { $('.togglediv').slideup(options.speed, options.easing); // once button clicked, clicked div (.togglediv) slides var togglediv = $(this).attr('rel'); // reads rel attribute of clicked link / div id toggle $(togglediv).slidetoggle(options.speed, options.easing); // toggle show/hide correct div + speed , easing e.preventdefault(); }); }; })(jquery); $('.show_hide').showhide({ speed: 200, // speed want toggle happen });
a simple way go this: add class 1 clicked, , disallow second click if link active.
$('.show_hide section').click(function() { if ($(this).hasclass('active')) return false; //use if want allow click after switching sections $('.active').removeclass('active'); $(this).addclass('active'); });
Comments
Post a Comment