instead of innerHTML
, a better way to insert a new element into your Document
Element.appendChild
var radio = document.createElement("input");
radio.type = "radio";
radio.name = "r1";
document.querySelector("#demo").appendChild(radio);
<label id="demo"></label>
Element.insertAdjacentHTML
document.querySelector("#demo").insertAdjacentHTML(
"beforeend",
"<input type='radio' name='r1'>"
);
<label id="demo"></label>
Available insertion points: 'beforebegin' 'afterbegin' 'beforeend' 'afterend'
innerHTML
will rewrite your target Element content, losing previous input states and attached events etc. Read more here