| | |
Function to Create Radio Button
Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
I have a button that activates a script to create a new radio button. I want the radio button to be of the class draggable. However, my code isn't quite working.
Any thoughts on this are appreciated.
Any thoughts on this are appreciated.
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<head> <script Language="JavaScript"> function modify(){ var newElem= document.createElement("radio") newElem.id = "newradio" newElem.appendChild(newElem) document.body.appendChild(newElem) document.getElementById("emphasis1").childNodes[0].nodeValue = "first " } </script> </head> <body> <button onClick="modify()">Add a radio</button> </body>
Last edited by Tom Tolleson; Oct 10th, 2007 at 11:51 am.
Do you mean CSS class? If so setAttribute should work. I've not tested this.
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
newElem.setAttribute("class", "draggable")
•
•
•
•
Do you mean CSS class? If so setAttribute should work. I've not tested this.
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
newElem.setAttribute("class", "draggable")
However, it still doesn't create a radio button on the page. I'm kind of a newbie, and any advice is appreciated.
You have to append it to the DOM once you have created it. It's an input element so I assume you want it in a form? rather than appending to the body element.
You have this ?????
I'm not sure what happens when you append an element to itself.
Also I think
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
document.forms[0].appendChild(newElem);
You have this ?????
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
newElem.appendChild(newElem)
I'm not sure what happens when you append an element to itself.
Also I think
document.createElement("radio") should be document.createElement("input") then set it's type to radio newElem.type = "radio" Last edited by hollystyles; Oct 10th, 2007 at 1:03 pm.
Thanks! Unfortunately the code
Creates a null when the code attempts the append child method:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
var newElem= document.createElement("input") newElem.type = "radio"
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
document.forms[0].appendChild(newElem);
Hmmm I've had a go myself. This is what I got:
It works great in Firefox, but in IE7 when I click a radio button it doesn't stay selected!
Anyone got any ideas?
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Untitled Page</title> <script type="text/javascript"> var radioNum = 1; function addRadio() { var radio = document.createElement("input"); radio.id = "radio" + radioNum; //Gotta love that loosley typed implicit conversion!!!! radio.setAttribute("type", "radio"); radio.setAttribute("name", "radios"); radio.value = "myvalue"; //radio.setAttribute("onclick", function(){alert(this.value);}); document.getElementById("divRadiolist").appendChild(radio); radioNum++; } </script> </head> <body> <form id="form1" method="POST" action="#"> <input type="button" id="btnAddRadio" value="Add radio" onclick="addRadio();" /> <div id="divRadiolist"></div> </div> </form> </body> </html>
It works great in Firefox, but in IE7 when I click a radio button it doesn't stay selected!
Anyone got any ideas?
Last edited by hollystyles; Oct 10th, 2007 at 1:47 pm.
![]() |
Similar Threads
- How to retrieve data from a dynamically generated radio button (ASP.NET)
- radio button (ASP.NET)
- retrieving ids from dynamically created controls in asp.net (ASP)
- Want to use radio button or menu bar instead of combo box (Java)
- Am not able to get a radio button selection to display in output (PHP)
- Security Settings -> Java permission -> Custom (radio button) -> Java Custom Settings (Web Browsers)
- Problem passing value from radio button to second page. (PHP)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: Light Box Not Working.
- Next Thread: Are jquery pagination are reliable with large data
| Thread Tools | Search this Thread |
Tag cloud for JavaScript / DHTML / AJAX
ajax ajaxexample ajaxjspservlets array autoplay blackjack browser captchaformproblem checkbox child class close codes date debugger dependent developer disablefirebug dom editor element embed engine events explorer ext file firefox flash form forms game gears getselection google gxt hiddenvalue highlightedword hint html ie7 ie8 iframe internet java javascript javascripthelp2020 jquery jsf jsfile jsp jump libcurl maps marquee masterpage math matrixcaptcha media mysql object onerror onmouseoutdivproblem onreadystatechange parent passing paypal pdf php player position post programming rated redirect runtime safari scriptlets scroll search security session shopping size software solutions star stars stretch synchronous toggle tweet unicode variables web webkit webservice window wysiwyg \n






