Skip to main content

Radio Group

<gstock-radio-group> | GstockRadioGroup
Option 1 Option 2 Option 3
<gstock-radio-group label="Select an option" name="a" value="1">
  <gstock-radio value="1">Option 1</gstock-radio>
  <gstock-radio value="2">Option 2</gstock-radio>
  <gstock-radio value="3">Option 3</gstock-radio>
</gstock-radio-group>

Help Text

Add descriptive help text to a radio group with the help-text attribute. For help texts that contain HTML, use the help-text slot instead.

Option 1 Option 2 Option 3
<gstock-radio-group label="Select an option" help-text="Choose the most appropriate option." name="a" value="1">
  <gstock-radio value="1">Option 1</gstock-radio>
  <gstock-radio value="2">Option 2</gstock-radio>
  <gstock-radio value="3">Option 3</gstock-radio>
</gstock-radio-group>

Radio Buttons

Radio buttons offer an alternate way to display radio controls. In this case, an internal button group is used to group the buttons into a single, cohesive control.

Option 1 Option 2 Option 3
<gstock-radio-group label="Select an option" help-text="Select an option that makes you proud." name="a" value="1">
  <gstock-radio-button value="1">Option 1</gstock-radio-button>
  <gstock-radio-button value="2">Option 2</gstock-radio-button>
  <gstock-radio-button value="3">Option 3</gstock-radio-button>
</gstock-radio-group>

Disabling Options

Radios and radio buttons can be disabled by adding the disabled attribute to the respective options inside the radio group.

Option 1 Option 2 Option 3
<gstock-radio-group label="Select an option" name="a" value="1">
  <gstock-radio value="1">Option 1</gstock-radio>
  <gstock-radio value="2" disabled>Option 2</gstock-radio>
  <gstock-radio value="3">Option 3</gstock-radio>
</gstock-radio-group>

Sizing Options

The size of Radios and Radio Buttons will be determined by the Radio Group’s size attribute.

Small Medium Large
<gstock-radio-group label="Select an option" size="medium" value="medium" class="radio-group-size">
  <gstock-radio value="small">Small</gstock-radio>
  <gstock-radio value="medium">Medium</gstock-radio>
  <gstock-radio value="large">Large</gstock-radio>
</gstock-radio-group>

<script type="module">
  const radioGroup = document.querySelector('.radio-group-size');

  radioGroup.addEventListener('gstock-change-event', () => {
    radioGroup.size = radioGroup.value;
  });
</script>

Validation

Setting the required attribute to make selecting an option mandatory. If a value has not been selected, it will prevent the form from submitting and display an error message.

Option 1 Option 2 Option 3
Submit
<form class="validation">
  <gstock-radio-group label="Select an option" name="a" required>
    <gstock-radio value="1">Option 1</gstock-radio>
    <gstock-radio value="2">Option 2</gstock-radio>
    <gstock-radio value="3">Option 3</gstock-radio>
  </gstock-radio-group>
  <br />
  <gstock-button type="submit" color="primary">Submit</gstock-button>
</form>

<script type="module">
  const form = document.querySelector('.validation');

  form.addEventListener('submit', event => {
    event.preventDefault();
    const formData = JSON.stringify(Object.fromEntries(new FormData(form)));
    alert(`The field is valid!\n\n${formData}`);
  });
</script>

Custom Validity

Use the setCustomValidity() method to set a custom validation message. This will prevent the form from submitting and make the browser display the error message you provide. To clear the error, call this function with an empty string.

Not me Me neither Choose me
Submit
<form class="custom-validity">
  <gstock-radio-group label="Select an option" name="a" value="1">
    <gstock-radio value="1">Not me</gstock-radio>
    <gstock-radio value="2">Me neither</gstock-radio>
    <gstock-radio value="3">Choose me</gstock-radio>
  </gstock-radio-group>
  <br />
  <gstock-button type="submit" color="primary">Submit</gstock-button>
</form>

<script type="module">
  const form = document.querySelector('.custom-validity');
  const radioGroup = form.querySelector('gstock-radio-group');
  const errorMessage = 'You must choose the last option';

  customElements.whenDefined('gstock-radio').then(() => {
    radioGroup.setCustomValidity(errorMessage);
  });

  form.addEventListener('gstock-change-event', () => {
    const isValid = radioGroup.value === '3';
    radioGroup.setCustomValidity(isValid ? '' : errorMessage);
  });

  form.addEventListener('submit', event => {
    event.preventDefault();
    const formData = JSON.stringify(Object.fromEntries(new FormData(form)));
    alert(`The field is valid!\n\n${formData}`);
  });
</script>