Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Set the <code>checked</code> attribute like so:</p> <pre><code>document.getElementById('radioId').checked = 'checked'; </code></pre> <p>Inversely, you can remove the attribute to <em>uncheck</em> it:</p> <pre><code>document.getElementById('radioId').removeAttribute('checked'); </code></pre> <p>Write example:</p> <pre><code>&lt;input id="maleRadioBox" type="radio" name="sex" value="male"&gt;Male&lt;br&gt; &lt;input id="femaleRadioBox" type="radio" name="sex" value="female" checked&gt;Female&lt;/input&gt; [...] if( values.maleChecked ) document.getElementById('maleRadioBox').checked = 'checked'; else document.getElementById('maleRadioBox').removeAttribute('checked'); if( values.femaleChecked ) document.getElementById('femaleRadioBox').checked = 'checked'; else document.getElementById('femaleRadioBox').removeAttribute('checked'); </code></pre> <p>Read example:</p> <pre><code>&lt;input id="maleRadioBox" type="radio" name="sex" value="male" checked&gt;Male&lt;br&gt; &lt;input id="femaleRadioBox" type="radio" name="sex" value="female"&gt;Female&lt;/input&gt; [...] if( document.getElementById('maleRadioBox').hasAttribute('checked') ) values.maleChecked = 'checked'; // or maybe values.maleChecked = document.getElementById('maleRadioBox').hasAttribute('checked'); if( document.getElementById('femaleRadioBox').hasAttribute('checked') ) values.femaleChecked = 'checked'; // or maybe values.femaleChecked = document.getElementById('femaleRadioBox').hasAttribute('checked'); </code></pre> <p>You could always make a function to simplify it:</p> <pre><code>function updateRadioInput( id, checked ) { if( checked ) document.getElementById( id ).checked = 'checked'; else document.getElementById( id ).removeAttribute('checked'); } updateRadioInput( 'maleChecked', values.isMaleChecked ); updateRadioInput( 'femaleChecked', values.isFemaleChecked ); // etc... </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload