the idea is having a button that, when clicked, adds a random class picking it from an array to another div. what i want is having that same button to remove any randomly added class from it when i re-click it.
here's a fiddle: https://jsfiddle.net/jLmj7bwk/1/
and this is the function
$(".button").click(function() {
var classes = ['one', 'two', 'three'];
var randomnumber = Math.floor(Math.random() * classes.length);
$(".element").toggleClass(classes[randomnumber]);
if ($(this).text() == "cancel added class")
$(this).text("add random class")
else
$(this).text("cancel added class");
});
this does add a class, but it keeps on adding them without removing it. could you help? thanks a lot!