Dynamically created radio inputs are not checked in IE7

I came across a strange IE error. It seems that when creating radio inputs with document.createElement, the resulting widgets do not respond to clicks. They sow for a second when you click on them, but they are not checked. It works as expected in FF3, but not in IE7. Any idea what's wrong with that?

<html> <body> <form> <div id="foo"> </div> </form> <script> var foo = document.getElementById('foo'); var t = document.createElement('input'); t.type='radio'; t.name ='fool'; var f = document.createElement('input'); f.type='radio'; f.name ='fool'; foo.appendChild(t); foo.appendChild(f); </script> </body> </html> 
+4
source share
1 answer

For some reason, creating switches like this in IE doesn't work.

A solution that seems to work (according to a found article here ):

 var r; try { // This works in IE r = document.createElement('<input type="radio" name="foo1"/>'); } catch( err ) { // For everyone else r = document.createElement('input'); } r.setAttribute('type', 'radio'); r.setAttribute('name', 'foo1'); 
+6
source

Source: https://habr.com/ru/post/1286769/


All Articles