Display element from the set using probability (Vanilla JS)

This is simple Vanilla JS script that will allow you to show one element from the set using basic percentage probability.

Usage instructions:

  • Add js-randomize class to root element,
  • Add js-random to every element in the set,
  • Optionally you can set data-display for desired value of CSS display (block by default),
  • Finally set probability using data-probability on every element (it’s integer without percent sign).

Warning! If you want want compatibility with other browsers use Babelify or any other polyfill for NodeList.prototype.forEach.