addclass - JQuery swapping classes -
I am learning JQuery and when I write my own small scripts, then there has been a problem. So I have a navigation bar and every time a button is pressed, it should change its CSS class from 'BTN' to 'ActiveButton'. For testing purposes, I just want to be able to click on that button, and switch it back, though it does not. Does JQuery not update classes right now? If so, how do I force JQuery to do this?
& lt; Div class = "btn" & gt; Hello & lt; / Div & gt; & Lt; Div class = "btn" & gt; World & lt; / Div & gt; & Lt; Script & gt; $ (".btn") .click (function () {$ (this) .removeClass ('btn'); $ (this) .addClass ('activebtn');}); $ (".activebtn") .click (function () {$ (this) .removeClass ('activebtn'); $ (this) .addClass ('btn');}); & Lt; / Script & gt;
Just be able to do this:
$ (".btn"). Click (function () {$ (this) .toggleClass ('btn activebtn');});
Comments
Post a Comment