I have a link like this :
<a href="#" onclick="renkDegistir()" title="Yaz .......
And JS code :
function renkDegistir()
{
$("#cont #main article").addClass("siyah-stil");
}
When click this link siyah-stil class is added to article. But i want to do, if link clicked 2nd time remove siyah-stil class.
In summary ,
on first click : addClass("siyah-stil");
on second click : removeClass("siyah-stil");
Try using
toggleClass():But I’d suggest also removing the in-line
onclickattribute, and switching to jQuery’sclick()event-handler:Edited to address @Eray’s comment (below):
The reason I prefer to use non-inline code is that it reduces the complexity of changing the
onClickevents; since they get changed in one place, rather than having to change theonclickattribute in everya(or other) element. Admittedly, you could achieve that benefit by simply changing therenkDegistir()function, but then you end up with functions named for posterity, rather than the inherent nature of the function.It also makes it easier for others to take over, and adapt your code, and to iron out bugs when they appear.