javascript - jQuery selector eq:() not working -
i made toggle button pure css/jquery , works perfectly. problem comes when duplicated , tried toggle it. supposed, toggles 'toggled' @ same time, here code far:
html
<div id="container"> <div id="switch-1"><div class="light-1"></div><div class="switch"><div class="space"><div class="circle"></div></div></div><div class="light-2"></div></div><br><br> <div id="switch-2"><div class="light-1"></div><div class="switch"><div class="space"><div class="circle"></div></div></div><div class="light-2"></div></div> </div>
jquery
$(function(){ $('.space').click(function(){ if($('.circle').hasclass("detector")){ $('.circle').animate({ marginleft: "2px"}, "slow", function(){$('.light-1').css("background","#8e3135"); $('.light-2').css("background","#adafb2"); $('.circle').removeclass("detector");}); } else { $('.circle').animate({ marginleft: "47px"}, "slow", function(){$('.light-1').css("background","#adafb2"); $('.light-2').css("background","#8e3135"); $('.circle').addclass("detector");}); } }); $('.space').eq(1).click(function(){ if($('.circle').eq(1).hasclass("detector-1")){ $('.circle').eq(1).animate({ marginleft: "2px"}, "slow", function(){$('.light-1').eq(1).css("background","#8e3135"); $('.light-2').eq(1).css("background","#adafb2"); $('.circle').eq(1).removeclass("detector-1");}); } else { $('.circle').eq(1).animate({ marginleft: "47px"}, "slow", function(){$('.light-1').eq(1).css("background","#adafb2"); $('.light-2').eq(1).css("background","#8e3135"); $('.circle').eq(1).addclass("detector-1");}); } }); });
or jsfiddle:
this how works, when click toggle detects if has class called "detector". if doesn't, animates toggle , creates one. if does, means class created animates toggle , removes class.
ok, problem starts when duplicate toggle. have 2 of them want activate individually. easiest solution using :eq()
jquery selector or .eq()
jquery function people classified more 'correct' option.
so add code of second toggle didn't worked. in fiddle above can test yourself. please if know problems, let me know, thanks!
edit: used :eq()
selector didn't work either.
edit 2: use different detector class called "detector-1" prevent interfering other one.
$(function () { //the click function every element .space class $('.space').click(function () { //check on .circle child of clicked .space using "this" if ($('.circle', this).hasclass("detector")) { //and animate $('.circle', this).animate({ marginleft: "2px" }, "slow", function () { // since in animate callback, "this" // .circle of clicked .space // want lights change - have travel dom upwards // 1st .parent() brings .space // 2nd .parent() leads .switch // siblings() let find .light-1 element $(this).parent().parent().siblings('.light-1').css("background", "#8e3135"); // same here light-2 $(this).parent().parent().siblings('.light-2').css("background", "#adafb2"); $(this).removeclass("detector"); }); } else { $('.circle', this).animate({ marginleft: "47px" }, "slow", function () { $(this).parent().parent().siblings('.light-1').css("background", "#adafb2"); $(this).parent().parent().siblings('.light-2').css("background", "#8e3135"); $(this).addclass("detector"); }); } }); });
using selector, need define click handler once - , still works endless numbers of buttons... "see working fiddle"
forgot mention. changed css in fiddle, since background image didn't show up, created white circle via css...
Comments
Post a Comment