944,001 Members | Top Members by Rank

Ad:
Oct 10th, 2007
0

Function to Create Radio Button

Expand Post »
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.
Reputation Points: 10
Solved Threads: 0
Light Poster
Tom Tolleson is offline Offline
39 posts
since Oct 2007
Oct 10th, 2007
0

Re: Function to Create Radio Button

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")
Reputation Points: 262
Solved Threads: 68
Veteran Poster
hollystyles is offline Offline
1,181 posts
since Feb 2005
Oct 10th, 2007
0

Re: Function to Create Radio Button

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
Tom Tolleson is offline Offline
39 posts
since Oct 2007
Oct 10th, 2007
0

Re: Function to Create Radio Button

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.
Reputation Points: 262
Solved Threads: 68
Veteran Poster
hollystyles is offline Offline
1,181 posts
since Feb 2005
Oct 10th, 2007
0

Re: Function to Create Radio Button

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);
Reputation Points: 10
Solved Threads: 0
Light Poster
Tom Tolleson is offline Offline
39 posts
since Oct 2007
Oct 10th, 2007
0

Re: Function to Create Radio Button

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.
Reputation Points: 262
Solved Threads: 68
Veteran Poster
hollystyles is offline Offline
1,181 posts
since Feb 2005
Nov 20th, 2009
0

Radio connect

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; Nov 20th, 2009 at 7:49 am. Reason: website snipped
Reputation Points: 10
Solved Threads: 0
Newbie Poster
uthumiram is offline Offline
1 posts
since Nov 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Light Box Not Working.
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Are jquery pagination are reliable with large data





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC