Dynamically creating a radio button using eg
var radioInput = document.createElement('input'); radioInput.setAttribute('type', 'radio'); radioInput.setAttribute('name', name);
works in Firefox but not in IE. Why not?
createElement("input"); radioYes. setAttribute("type", "radio"); /*Set id of new created radio button*/
Introduction to the JavaScript Radio Button To create a radio button, you use the <input> element with the type radio . A group of radio buttons is called a radio group. In this example, all the radio buttons have the same name size but different values. Because of this, you can only select one radio button at a time.
Taking a step from what Patrick suggests, using a temporary node we can get rid of the try/catch:
function createRadioElement(name, checked) { var radioHtml = '<input type="radio" name="' + name + '"'; if ( checked ) { radioHtml += ' checked="checked"'; } radioHtml += '/>'; var radioFragment = document.createElement('div'); radioFragment.innerHTML = radioHtml; return radioFragment.firstChild; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With