Solved

Problem Dynamically Creating a radio button with the DOM in IE6

Posted on 2004-04-26
10
836 Views
Last Modified: 2008-03-10
I have a list of static html radiobuttons, with name="heading" and id="heading" followed by a div(boxholder), that acts as a target area so I can create new radio buttons with javascript, when a form button is clicked.
The JS code is thus:

var headingtext = document.createTextNode(document.getElementById("headingtext").value);

var thediv = document.getElementById("boxholder");
var headingchkinput = document.createElement("input");
headingchkinput.name = "heading";
headingchkinput.id = "heading";
headingchkinput.type="radio";
headingchkinput.value=headingtext.nodeValue;
thediv.appendChild(headingchkinput);
thediv.appendChild(headingtext);
thediv.appendChild(br);

It works perfectly in Mozilla, but in IE6 - the new radio button appears, but it cant be selected - it just goes gray.

Any ideas?

0
Comment
Question by:MikebEdwards
  • 5
  • 4
10 Comments
 
LVL 10

Expert Comment

by:stu215
Comment Utility
try :

thediv.innerHTML.appendchild(<something>);

~Stu
0
 
LVL 10

Expert Comment

by:stu215
Comment Utility
err actually jus try changing this :

     var thediv = document.getElementById("boxholder");

To :

     var thediv = document.getElementById("boxholder").innerHTML;

------------------ or  try this method instead -----------------------------

var headingchkinput = document.createElement("input");
headingchkinput.name = "heading";
headingchkinput.id = "heading";
headingchkinput.type="radio";
headingchkinput.value=headingtext.nodeValue;
document.getElementById("boxholder").innerHTML.appendChild(headingchkinput);
document.getElementById("boxholder").innerHTML.appendChild(headingtext);
document.getElementById("boxholder").innerHTML.appendChild(br);

~Stu
0
 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
The problem in IE is that you cannot set the input element attribute "name".
And without name does radio button not work.
Setting the attribute "id" works in IE to be correctly submitted, but at creation time and before submitting is tha attribute "name" not accepted.
You can verify that by looking at generated innerHTML. As long as you give the attribute spelling some uppercase characters you will see the Name and NAME, but name will disappear from innerHTML.

The only solution for that bug is to create the input element by pasting the html code for the element.


0
 
LVL 10

Expert Comment

by:stu215
Comment Utility
Hmmm the following should insert a radio button that works into the div as i took it from a script i used b4:
----------------------------------------------------------------------
function myRadio(a, j){
      var tempVar="<input type=radio value=Yes name="+ myNames[a] + j +">Yes, <input type=radio value=No name="+ myNames[a] + j +">No<br>";
            return tempVar;
      }

function makeMyStuff () {
       var myNames= new array ("heading");

       var cntr=1;
       var temp="";

       for (i=0; i<myNames.length; i++;){
          temp=temp+myRadio(i, cntr);
          cntr++;
       }
       document..getElementById("boxholder").innerHTML=temp;
}
------------------------------------------------------------------------
to run it :


<script>
     makeMyStuff();
</script>
~Stu


0
 
LVL 10

Expert Comment

by:stu215
Comment Utility
Arg, put one to many dots in the line :

document..getElementById("boxholder").innerHTML=temp;

Change to :

document.getElementById("boxholder").innerHTML=temp;

~Stu
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:MikebEdwards
Comment Utility
I cant set innerHTML to a html-string of an input, because that will over-right each time - I want to add multiple radio buttons based on a form. It seems that Zvonko is right - IE wont let you set the 'name' attribute of an element - Using innerHTML.appendChild doesnt make a difference.

Which means that in IE - you cant dynamically add a radio button! At all!
0
 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
Yeap, you cannot, except by inserting innerHTML.

And please avoid the innerHTML.appendChild nonsense. That is not a valid statement.

0
 
LVL 10

Expert Comment

by:stu215
Comment Utility
it was just a thought... Ive never tried using that naming convention... I usually make an array of the names i want and then use a counter to add a # to the end for each record
activated by an (add a record) button which will allow you to have as many items (records) as you want on the page...

~Stu
0
 
LVL 63

Accepted Solution

by:
Zvonko earned 125 total points
Comment Utility
How about this one:

<html>
<head>
<script>
function addRadio(){
  var headingtext = document.createTextNode(document.getElementById("headingtext").value);
  var thediv = document.getElementById("boxholder");
  var headingchkinput = document.createElement("<input type=radio name='heading' id='heading' value='"+headingtext.nodeValue+"' >");
  //headingchkinput.name = "heading";
  //headingchkinput.id = "heading";
  //headingchkinput.type="radio";
  //headingchkinput.value=headingtext.nodeValue;
  thediv.appendChild(headingchkinput);
  thediv.appendChild(headingtext);
  var br = document.createElement("br");
  thediv.appendChild(br);
}
</script>
</head>
<body>
<form>
<input type=hidden name="headingtext" value="Label">
<input type=button value="Add" onClick="addRadio()">
<div id="boxholder"></div>
</form>
</body>
</html>


0
 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
And here an exampleonly for the proof of concept:

<html>
<head>
<script>
function addRadio(){
  var headingtext = document.createTextNode(document.getElementById("headingtext").value);
  var thediv = document.getElementById("boxholder");
  var headingchkinput = document.createElement("<input name='heading'>");
  //headingchkinput.name = "heading";
  headingchkinput.id = "heading";
  headingchkinput.type="radio";
  headingchkinput.value=headingtext.nodeValue;
  thediv.appendChild(headingchkinput);
  thediv.appendChild(headingtext);
  var br = document.createElement("br");
  thediv.appendChild(br);
alert(thediv.innerHTML)
}
</script>
</head>
<body>
<form>
<input type=hidden name="headingtext" value="Label">
<input type=button value="Add" onClick="addRadio()">
<div id="boxholder"></div>
</form>
</body>
</html>


0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now