Im trying to add a class to a specific id tag in my code. The class is kb1, kb2, kb3, or kb4. I want the JavaScript to pick a random number between 1 and 4 then apply that to the class to have it randomly add a class.
All of this is done in a loop so that it will constantly add and remove classes every 30 seconds.
EDIT: My apologies, i was trying to explain my problem and didnt add a question. For some reason when this runs nothing happens. No classes are added. Can you add classes based on random numbers, and if so why isnt what im trying to do working properly?
$(document).ready(function() {
function kbadd() {
number = 1 + Math.floor(Math.random() * 4);
$("#kb1").addClass("kb"+number);
$("#kb2").addClass("kb"+number);
$("#kb3").addClass("kb"+number);
$("#kb4").addClass("kb"+number);
timeoutID = window.setTimeout(kbremove(number), 30000);
}
function kbremove(number) {
$("#kb1").removeClass("kb"+number);
$("#kb2").removeClass("kb"+number);
$("#kb3").removeClass("kb"+number);
$("#kb4").removeClass("kb"+number);
timeoutID = window.setTimeout(kbadd, 1);
}
kbadd();
});
number? Are there any errors in the console?