Solved

Problem Dynamically Creating a radio button with the DOM in IE6

Posted on 2004-04-26
10
837 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
ID: 10922018
try :

thediv.innerHTML.appendchild(<something>);

~Stu
0
 
LVL 10

Expert Comment

by:stu215
ID: 10922058
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
ID: 10922234
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
ID: 10922379
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
ID: 10922396
Arg, put one to many dots in the line :

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

Change to :

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

~Stu
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:MikebEdwards
ID: 10926422
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
ID: 10927484
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
ID: 10928706
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
ID: 10929532
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
ID: 10929628
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

867 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

23 Experts available now in Live!

Get 1:1 Help Now