[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Problem Dynamically Creating a radio button with the DOM in IE6

Posted on 2004-04-26
10
Medium Priority
?
851 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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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

590 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