Languages
[Edit]
EN

HTML - radio input

3 points
Created by:
Palpys
764

In this article, we would like to show you how to create and use radio input elementin HTML.

Quick solution:

// ONLINE-RUNNER:browser;

<input type="radio" name="field-name" value="some-value" />

 

Notes:

  • radio input elements are assigned to groups by name attribute,
  • one that group creates single field,
  • only one radio button can be selected per field at the same time.

 

Practical examples

In this examples, we create radio input using the input element's type="radio" attribute, assigning elements for fields by name attributes.

 

1. Single field (one group)

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<body>
  Select color:
  <label><input type="radio" name="color" value="red" checked /> Red</label>
  <label><input type="radio" name="color" value="green" /> Green</label>
  <label><input type="radio" name="color" value="blue" /> Blue</label>
</body>
</html>

 

2. Multiple fields (two groups)

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<body>
  <div>
    Select color:
    <label><input type="radio" name="color" value="red" checked /> Red</label>
    <label><input type="radio" name="color" value="green" /> Green</label>
    <label><input type="radio" name="color" value="blue" /> Blue</label>
  </div>
  <div>
    Select animal:
    <label><input type="radio" name="animal" value="cat" checked /> Cat</label>
    <label><input type="radio" name="animal" value="dog" /> Dog</label>
    <label><input type="radio" name="animal" value="bird" /> Bird</label>
  </div>
</body>
</html>

Note: You can create more fields if needed, remembering to use different names.

 

See also

  1. HTML - select radio input on click on related text

References

  1. <input type="radio"> - HTML | MDN

Alternative titles

  1. HTML - input type radio
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

HTML - input element types

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join