JavaScript USES the this variable to quickly find out how the user selects the radio button

  • 2020-05-17 04:49:54
  • OfStack

The example in this article shows how JavaScript can quickly find the radio button selected by the user through the this variable. Share with you for your reference. The specific analysis is as follows:

The JS code below USES the this variable in combination with the onchange event of the radio button to quickly find out which radio button the user selected


<script>
function favAnimal(button) {
alert('You like '+button.value+'s.');
}
</script>
<input type="radio" name="marsupial" value="kangaroo"
    onchange="favAnimal(this)">Kangaroo
<br /><input type="radio" name="marsupial" value="Opossum"
    onchange="favAnimal(this)">Opossum
<br /><input type="radio" name="marsupial" value="Tasmanian Tiger"
    onchange="favAnimal(this)">Tasmanian Tiger

I hope this article has been helpful to your javascript programming.


Related articles: