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
Reply

Join Date: Oct 2007
Posts: 39
Reputation: Tom Tolleson is an unknown quantity at this point 
Solved Threads: 0
Tom Tolleson's Avatar
Tom Tolleson Tom Tolleson is offline Offline
Light Poster

Function to Create Radio Button

 
0
  #1
Oct 10th, 2007
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.

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <head>
  2. <script Language="JavaScript">
  3.  
  4. function modify(){
  5. var newElem= document.createElement("radio")
  6. newElem.id = "newradio"
  7. newElem.appendChild(newElem)
  8. document.body.appendChild(newElem)
  9. document.getElementById("emphasis1").childNodes[0].nodeValue = "first "
  10. }
  11. </script>
  12. </head>
  13. <body>
  14. <button onClick="modify()">Add a radio</button>
  15.  
  16. </body>
Last edited by Tom Tolleson; Oct 10th, 2007 at 11:51 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 1,181
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Solved Threads: 67
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: Function to Create Radio Button

 
0
  #2
Oct 10th, 2007
Do you mean CSS class? If so setAttribute should work. I've not tested this.

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. newElem.setAttribute("class", "draggable")
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 39
Reputation: Tom Tolleson is an unknown quantity at this point 
Solved Threads: 0
Tom Tolleson's Avatar
Tom Tolleson Tom Tolleson is offline Offline
Light Poster

Re: Function to Create Radio Button

 
0
  #3
Oct 10th, 2007
Originally Posted by hollystyles View Post
Do you mean CSS class? If so setAttribute should work. I've not tested this.

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. newElem.setAttribute("class", "draggable")
That works (no errors returning)! Thanks.

However, it still doesn't create a radio button on the page. I'm kind of a newbie, and any advice is appreciated.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 1,181
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Solved Threads: 67
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: Function to Create Radio Button

 
0
  #4
Oct 10th, 2007
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.

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. document.forms[0].appendChild(newElem);

You have this ?????
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. 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.
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 39
Reputation: Tom Tolleson is an unknown quantity at this point 
Solved Threads: 0
Tom Tolleson's Avatar
Tom Tolleson Tom Tolleson is offline Offline
Light Poster

Re: Function to Create Radio Button

 
0
  #5
Oct 10th, 2007
Thanks! Unfortunately the code

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. var newElem= document.createElement("input")
  2. newElem.type = "radio"
Creates a null when the code attempts the append child method:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. document.forms[0].appendChild(newElem);
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 1,181
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Solved Threads: 67
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: Function to Create Radio Button

 
0
  #6
Oct 10th, 2007
Hmmm I've had a go myself. This is what I got:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2.  
  3. <html xmlns="http://www.w3.org/1999/xhtml" >
  4. <head runat="server">
  5. <title>Untitled Page</title>
  6. <script type="text/javascript">
  7. var radioNum = 1;
  8.  
  9. function addRadio()
  10. {
  11. var radio = document.createElement("input");
  12. radio.id = "radio" + radioNum; //Gotta love that loosley typed implicit conversion!!!!
  13. radio.setAttribute("type", "radio");
  14. radio.setAttribute("name", "radios");
  15. radio.value = "myvalue";
  16. //radio.setAttribute("onclick", function(){alert(this.value);});
  17. document.getElementById("divRadiolist").appendChild(radio);
  18. radioNum++;
  19. }
  20.  
  21. </script>
  22. </head>
  23. <body>
  24. <form id="form1" method="POST" action="#">
  25.  
  26. <input type="button" id="btnAddRadio" value="Add radio" onclick="addRadio();" />
  27. <div id="divRadiolist"></div>
  28. </div>
  29. </form>
  30. </body>
  31. </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.
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 1
Reputation: uthumiram is an unknown quantity at this point 
Solved Threads: 0
uthumiram uthumiram is offline Offline
Newbie Poster

Radio connect

 
0
  #7
29 Days Ago
hi
i am prabhu
i start one site <website snipped>
i wnt one help , how connect radio to my site .
i have ip add. i want java code.
thank you
by
tamilofun team
Last edited by nav33n; 29 Days Ago at 7:49 am. Reason: website snipped
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



Tag cloud for JavaScript / DHTML / AJAX
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC