document.addEventListener('DOMContentLoaded', function(){ (function(){ let sr = document.querySelectorAll('.my-star'); let i = 0; //loop through stars while (i < sr.length){ //attach click event sr[i].addEventListener('click', function(){ //current star let cs = parseInt(this.getAttribute("data-star")); //output current clicked star value document.querySelector('#output').value = cs; })//end of click event i++; }//end of while loop })();//end of function})
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter