Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Just show the code:

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

<button onclick="show()">Show value</button>
like image 262
chesszhang Avatar asked Dec 24 '22 01:12

chesszhang


1 Answers

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>
like image 60
Mike Avatar answered Dec 25 '22 14:12

Mike