User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 456,528 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,752 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 3675 | Replies: 5
Reply
Join Date: Oct 2007
Location: Baltimore/DC
Posts: 33
Reputation: Tom Tolleson is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
Tom Tolleson's Avatar
Tom Tolleson Tom Tolleson is offline Offline
Light Poster

Function to Create Radio Button

  #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.

<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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2005
Location: Braintree, UK
Posts: 1,166
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Rep Power: 7
Solved Threads: 59
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: Function to Create Radio Button

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

newElem.setAttribute("class", "draggable")
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote  
Join Date: Oct 2007
Location: Baltimore/DC
Posts: 33
Reputation: Tom Tolleson is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
Tom Tolleson's Avatar
Tom Tolleson Tom Tolleson is offline Offline
Light Poster

Re: Function to Create Radio Button

  #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.

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  
Join Date: Feb 2005
Location: Braintree, UK
Posts: 1,166
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Rep Power: 7
Solved Threads: 59
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: Function to Create Radio Button

  #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.

document.forms[0].appendChild(newElem);

You have this ?????
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  
Join Date: Oct 2007
Location: Baltimore/DC
Posts: 33
Reputation: Tom Tolleson is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
Tom Tolleson's Avatar
Tom Tolleson Tom Tolleson is offline Offline
Light Poster

Re: Function to Create Radio Button

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

	var newElem= document.createElement("input")
	newElem.type = "radio"
Creates a null when the code attempts the append child method:
	document.forms[0].appendChild(newElem);
Reply With Quote  
Join Date: Feb 2005
Location: Braintree, UK
Posts: 1,166
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Rep Power: 7
Solved Threads: 59
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: Function to Create Radio Button

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

<!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.
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb JavaScript / DHTML / AJAX Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum

All times are GMT -4. The time now is 4:22 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC