html - jQuery pick a block and change it's bg color with the bg color of one of the blocks from list -
for example in 1 column have 3 blocks (div), , in second column have list of other blocks (div).
the first column blocks represent cake floors , second column blocks represent tastes can applied of cake floor's.
so picking floor want pick taste 1 floor, , then, picking floor, want apply 1 of other tastes...
something that:
example each taste's title, doesn't work properly, since taste applies selected floors.
$(".cake-floor").click(function(){ var floor = $(this); $(".cake-taste").click(function(){ var taste = $(this).text(); $(floor).text(taste); }); });
html:
<div class="col-md-6 mb-sm-3"> <div style="background: #dedede; height: 40px; width: 40px; border-radius: 50%; margin-bottom: -10px;"></div> <div class="cake-floor mb-2" data-floor="3"></div> <div class="cake-floor mb-2" data-floor="2"></div> <div class="cake-floor" data-floor="1"></div> </div> <div class="col-md-6 tastes"> <div class="cake-taste text-center" data-taste="chocolate">chocolate</div> <div class="cake-taste text-center" data-taste="caramel">caramel</div> <div class="cake-taste text-center" data-taste="banana">banana</div> <div class="cake-taste text-center" data-taste="lime">lime</div> <div class="cake-taste text-center" data-taste="blueberry">blueberry</div> <div class="cake-taste text-center" data-taste="rapsberry">rapsberry</div> <div class="cake-taste text-center" data-taste="strawberry">strawberry</div> </div>
when first time div.cake-floor
selected - adds event listener $(".cake-taste").click
event.
when div.cake-floor
selected again new event gets added. there 2 methods listening event , multiple divs getting updated.
this can resolved using off
event of jquery
.
$(".cake-taste").off().click(function(){ ... }
refer jsfiddle here
Comments
Post a Comment