i have this HTML.
<a href="#" class="button redB" style="margin: 4px 4px 4px -4px;" id="estEditItemToggleMode" data-mode="edit">
<img src="images/icons/light/check.png" alt="" class="icon">
<span>Enable Editing Mode</span>
</a>
on click i want to keep toggling the value of class attribute and data-mode
it should go like this
if(mode == "edit") {
class = "button blueB";
data-mode = "select";
}
else if(mode == "select") {
class = "button redB";
data-mode = "edit";
}
i tried using this.
$('#estEditItemToggleMode').live('click', function(e){
e.preventDefault();
if($(this).data('mode') == "edit") {
$(this).removeClass('redB').addClass('blueB');
$(this).attr('data-mode', 'select');
}
else if($(this).data('mode') == "select") {
$(this).removeClass('blueB').addClass('redB');
$(this).attr('data-mode', 'edit');
}
});
this toggle the values only once. and hence no effect on two or more click. how to go with this correctly.
Try like below,
DEMO
and remove the attr
data-modefrom the markup.Edit: Cached some redundant jQuery calls.
Edit 2:
.liveis deprecated so you should either use.onor.delegateEdit 3: Fiddle output,