Solved

Problem Dynamically Creating a radio button with the DOM in IE6

Posted on 2004-04-26
10
840 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

730 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