Why the input[type=radio]'s defalut value is 'on' instead of ''?

  • A+
Category:Languages

Just show the code:

function show() {   console.log(document.querySelector('input[type=radio]').value) // 'on' }
<input type="radio">  <button onclick="show()">Show value</button>

 


It's the default value for radio and checkbox input. It does not mean that radio button is currently "on". The property you would want for that is checked.

<input type="radio" value="Another Value"> <script>   console.log(document.querySelector('input[type=radio]').checked)   console.log(document.querySelector('input[type=radio]').value) // 'on' </script>

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: