javascript - Jquery click event targeting more than one element -
i have 2 triggers on image. general behaviour:
- when click trigger, should reveal small context box (got working)
- when click outside of context box or trigger, should disappear (got working)
- when click trigger, if there trigger/context box active/open, should close other 1 , open click 1 (not working)
here's html:
<span class='pulse-button' id="button-1"></span> <div class="content-box context-closed tabs" id="tabs1"> </div> here's jquery:
$(function(){ $(".pulse-button").click(function(e){ e.stoppropagation(); if( $(".pulse-button").not(this).hasclass("pulse-button-active")){ $(".pulse-button").not(this).removeclass("pulse-button-active"); $(".pulse-button").not(this).next().addclass("context-closed"); }else{ $(this).addclass("pulse-button-active"); $(this).next().removeclass("context-closed"); }; }); $("body").click(function(evt){ if(evt.target.class == "content-box") return; if($(evt.target).closest('.content-box').length) return; if( $(".pulse-button").hasclass("pulse-button-active")){ $(".pulse-button").removeclass("pulse-button-active"); $(".pulse-button").next().addclass("context-closed"); }; }); $( "#tabs1,#tabs2" ).tabs(); }); what doing wrong here?
you don't need else in .pulse-button click. because code in else block should executed either .pulse-button has class pulse-button-active or not. change click event following.
$(".pulse-button").click(function(e){ e.stoppropagation(); if($(".pulse-button").not(this).hasclass("pulse-button-active")) { $(".pulse-button").not(this).removeclass("pulse-button-active"); $(".pulse-button").not(this).next().addclass("context-closed"); } $(this).addclass("pulse-button-active"); $(this).next().removeclass("context-closed"); });
Comments
Post a Comment