faqts : Computers : Programming : Languages : JavaScript : Forms : Radio buttons

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

23 of 25 people (92%) answered Yes
Recently 10 of 10 people (100%) answered Yes

Entry

How to use radiobuttons that were created in code (look right but don't work)?

Mar 17th, 2003 06:59
Keith Fearnley, MSDN


If you try creating INPUT elements dynamically using code like the 
following:
 <SCRIPT LANGUAGE=javascript>
 var p = document.getElementById("paragraphID");
 var r = document.createElement("INPUT");
 r.name="radio";     // This line does not work quite right!
 r.type="radio";
 r.id="radio3";
 r.value="rc";
 r.disabled=false;
 p.appendChild(r);
 //-->
 </SCRIPT>

... you will find that the name doesn't work quite right (although code 
to show the name will make it appear right).  The radio button behaves 
as a static-created radio button would if no name was given (i.e. it 
greys but will not check).
To get around this, specify the name in the createElement call as 
follows instead of the commented line above:

var r = document.createElement("<INPUT name='radio'>");

See MSDN for createElement() call for details.