jquery - Exchanging classes between two divs when clickin on one of them -
i have code below. when run app , click on "bar", message the not 'current' div has been clicked shown. if click on "foo", message not shown. expected message shown, since have exchanged class current between 2 divs.
<div class="current">foo</div> <div>bar</div> $('document').ready(function(){ $('div').not('.current').on('click', function(){ alert("the not 'current' div has been clicked.") var aux = $(this); $('div').removeclass('current'); aux.addclass('current'); }); });
on dom ready, have assigned click event div not having class current. hence, code doesn't work. need assign click events div's , check class inside like:
$('div').on('click', function (e) { if (e.target.classname === 'current') return; console.log("the not 'current' div has been clicked.") var $aux = $(this); $('div').removeclass('current'); $aux.addclass('current'); });
Comments
Post a Comment