'In HTML, with JavaScript, create new radio button and its text?

I want that when the "Yes" radio button of a form (form1) is checked, a new form (form2) appears, with two radio buttons and their text "Yes" and "No". With an event "onclick" in the "Yes" button of the form1, I manage to make the new form appear, with the two radio buttons, but I cannot make their text appear. Since radio buttons do not have "innerHTML", I try to add the text either as plain text, either as "label", but it is not working. Is it a problem in the syntax or in the logic (not possible to create text at the same time as the button)?

In my HTML body I have this:

<form id="form1">
<input type="radio" id= "form1_no" value="no" checked>
   <label for = "form1_no" >No</label>
<input type="radio" id= "form1_yes" value="yes" onClick= exam()>
   <label for = "form2_yes" >Yes</label>
</form> 

The function exam() is:

<script type='application/javascript'>

function exam() {                  
                    
    var inputno = document.createElement("input");
    inputno.type = "radio";
    inputno.id = "form2_no";
    inputno.value = "no";
    inputno.onclick = function () {alert("I select No in Form 2")}; 
    document.getElementById("form2").appendChild(inputno); // this is working

    var inputyes = document.createElement("input");
    inputyes.type = "radio";
    inputyes.id = "form2_yes";
    inputyes.value  ="yes";
    inputyes.onclick = function () {alert("I select Yes in Form 2")};
    document.getElementById("form2").appendChild(inputyes); // this is working

    // now, the code that is not working:

    // 1st tentative (adding "Yes" and "No" as plain text after their radio button):
    var textno = "No";
       document.getElementById("form2_no").appendChild(textno);
    var textyes = "Yes";
       document.getElementById("form2_yes").appendChild(textyes);
      
    // 2nd tentative (adding "Yes" and "No" as labels to their radio button):
    var labelno = document.createElement("label"); 
    labelno.for="form2_no"; 
    labelno.innerHTML = "No"; 
    document.getElementById("form2_no").appendChild(labelno);
    var labelyes = document.createElement("label"); 
    labelyes.for="form2_yes"; 
    labelyes.innerHTML = "Yes"; 
    document.getElementById("form2_yes").appendChild(labelyes);
    
}      
</script>


Solution 1:[1]

Something like this works to create a button and label.

<div id="radio_home"></div>

<script>

  var radio_home = document.getElementById("radio_home");

  function makeRadioButton(name, value, text) {

    var label = document.createElement("label");
    var radio = document.createElement("input");
    radio.type = "radio";
    radio.name = name;
    radio.value = value;

    label.appendChild(radio);

    label.appendChild(document.createTextNode(text));
    return label;
  }

  var yes_button = makeRadioButton("yesbutton", "yes", "Oh yea! do it!");
  radio_home.appendChild(yes_button);
</script>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jeremy J Starcher