Example
<div class="room" id="kitchen"> <div class="lightbulb on"></div> <div class="switch"></div> <div class="switch"></div> <div class="clapper"></div> </div>
$( ".switch, .clapper" ).click(function() { var light = $( this ).closest( ".room" ).find( ".lightbulb" ); if ( light.is( ".on" ) ) { light.removeClass( "on" ).addClass( "off" ); } else { light.removeClass( "off" ).addClass( "on" ); } });
$( ".lightbulb" ).on( "light:toggle", function( event ) { var light = $( this ); if ( light.is( ".on" ) ) { light.removeClass( "on" ).addClass( "off" ); } else { light.removeClass( "off" ).addClass( "on" ); } }); $( ".switch, .clapper" ).click(function() { var room = $( this ).closest( ".room" ); room.find( ".lightbulb" ).trigger( "light:toggle" ); });