?
Solved

Problem Dynamically Creating a radio button with the DOM in IE6

Posted on 2004-04-26
10
Medium Priority
?
843 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 

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 375 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…
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…
Suggested Courses

770 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