html – How to set radio button selected by default?

Question:

I have the following code in HTML

   <label class="radio-inline">
      <input type="radio" name="movimiento" value="Venta">Venta</label>
   <label class="radio-inline">
      <input type="radio" name="movimiento" value="Renta" required> Renta </label>
   <label class="radio-inline">
      <input type="radio" name="movimiento" value="Traspaso"> Traspaso </label>

And I have a database that is loaded with its respective data, how can I preselect the checkbox that is entered in the database? That is, if in the database I select Sale, how can the Sale checkbox appear selected?

Answer:

To set the selected radio button by default, you must use the checked attribute:

    <label class="radio-inline">
          <input type="radio" name="movimiento" value="Venta">Venta</label>
       <label class="radio-inline">
          <input type="radio" name="movimiento" value="Renta" required> Renta </label>
       <label class="radio-inline">
          <input type="radio" name="movimiento" value="Traspaso" checked> Traspaso (valor default)</label>

The checked attribute can be on <input type="checkbox"> and <input type="radio"> elements.

Scroll to Top