Skip to content

Instantly share code, notes, and snippets.

@Megafry
Created October 20, 2020 09:50
Show Gist options
  • Save Megafry/62e557b13c234c003299cf3699aa1262 to your computer and use it in GitHub Desktop.
Save Megafry/62e557b13c234c003299cf3699aa1262 to your computer and use it in GitHub Desktop.
Adds the given class to 1 element from the selection and return it:
// extend jQuery:
$.fn.setClassRand = function(cssClass) {
return this.eq( Math.floor( Math.random() * this.length ) ).addClass(cssClass);
};
//usage:
//add the class "teaser__video--active" and play it:
var video = $(".teaser__video").setClassRand("teaser__video--active");
if (video.length) {
video[0].play();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment