How can the use of radio buttons and JavaScript onClick events be optimized to update specific values dynamically in a form based on user selection?

To optimize the use of radio buttons and JavaScript onClick events to update specific values dynamically in a form based on user selection, you can create a function that is triggered by the onClick event of the radio buttons. Within this function, you can check the value of the selected radio button and update the specific values in the form accordingly.

<script>
function updateValue() {
  var radioButtons = document.getElementsByName('option');
  
  for (var i = 0; i < radioButtons.length; i++) {
    if (radioButtons[i].checked) {
      if (radioButtons[i].value === 'option1') {
        document.getElementById('specificValue').value = 'Value 1';
      } else if (radioButtons[i].value === 'option2') {
        document.getElementById('specificValue').value = 'Value 2';
      }
    }
  }
}
</script>

<form>
  <input type="radio" name="option" value="option1" onClick="updateValue()"> Option 1
  <input type="radio" name="option" value="option2" onClick="updateValue()"> Option 2

  <input type="text" id="specificValue">
</form>