Is there a cleaner and less messy way to do a on click to get a value of a radio button?

<div class="stars">
  <form action="">
    <input class="star star-5" id="star-5" type="radio" name="star" onclick="test(5);"/>
    <label class="star star-5" for="star-5"></label>
    <input class="star star-4" id="star-4" type="radio" name="star" onclick="test(4);"/>
    <label class="star star-4" for="star-4"></label>
    <input class="star star-3" id="star-3" type="radio" name="star" onclick="test(3);"/>
    <label class="star star-3" for="star-3"></label>
    <input class="star star-2" id="star-2" type="radio" name="star" onclick="test(2);"/>
    <label class="star star-2" for="star-2"></label>
    <input class="star star-1" id="star-1" type="radio" name="star" onclick="test(1);"/>
    <label class="star star-1" for="star-1"></label>
  </form>
</div>

<script> 
      
    function test(value)
    {
          alert(value);
    }
        
</script> 

The above is what i have right now which works fine but I don’t like how it has the onclick method on the input is there i cleaner and easier and better way to do this?

63 thoughts on “Is there a cleaner and less messy way to do a on click to get a value of a radio button?”

Leave a Comment