Solved

DOM Radio buttons in Netscape/Mozilla

Posted on 2003-11-10
6
1,447 Views
Last Modified: 2013-11-19
Can anyone tell me why this code doesn't work in Netscape/Mozilla?

document.body.appendChild(document.createElement("<input name=me type=radio>"));

It works fine in IE.
0
Comment
Question by:brgivens
  • 2
  • 2
  • 2
6 Comments
 
LVL 16

Expert Comment

by:jaysolomon
ID: 9717303
Dunno why NS does not work. But try this

<script type="text/javascript">
<!--
function Orefresh() {
document.getElementById('myForm').innerHTML += "<input type='radio' name='me'>";
}
// -->
</script>
</head>
<body>
<A HREF style="cursor: hand" onclick="Orefresh();return false;" class="Normal">
TEST</a><br />

<div id="myForm">
</div>

</body>
0
 
LVL 12

Accepted Solution

by:
ahosang earned 50 total points
ID: 9717457
Didn't work because that's not proper DOM creating standard. The standards can all be found at www.w3c.org under DOM and then DOM Technical reports.
More like:
var txt=document.createElement("input");
txt.name="mytext";
txt.type="radio";
document.forms['myform'].appendChild(txt);
0
 
LVL 16

Expert Comment

by:jaysolomon
ID: 9717537
PLEASE EXCEPT AHOSANG ANSWER AS HIS IS CORRECT

Your right ahosang

now that i look at it again i see the problem.

<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
<!--
function ee(){
var txt=document.createElement("input");
txt.name="mytext";
txt.type="radio";
document.forms['myform'].appendChild(txt);
}
// -->
</script>
</head>
<body>
<form name="myform" method="post" action="">
  <input type="button" name="btn" value="Add" onclick="ee();">
</form>
</body>
</html>
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 7

Author Comment

by:brgivens
ID: 9717716
OK, ahosang's solution is correct (for Netscape), but I want to point out that it doesn't work in IE.  IE doesn't allow you to set the name property after the element has been created.  The only way (that I've found) to actually set the name property in IE is to include it in the createElement parameter; ie document.createElement("<tag> name=myName>");

If anyone knows of another way to set the name property of element in IE, I'm all ears.

Here's a script to illustrate:

<html>
<head>
<link rel="stylesheet" type="text/css" href="sophie/sophie.css" />
<script src="sophie/toolbox.js"></script>
<script language="javascript">

function test() {
  var o = document.createElement("input");
  o.name = "me";
  o.type = "radio";
  document.body.appendChild(o);
  o = document.createElement("input");
  o.name = "me";
  o.type = "radio";
  document.body.appendChild(o);
}

</script>
</head>
<body onload="test();">
</body>
</html>

IE will render the script and if you examine the name property of either control, it will report "me", but when you click on one of the buttons, you'll notice neither button can be set.  This script works fine:

function test() {
  document.body.appendChild(document.createElement("<input name=me type=radio>"));
  document.body.appendChild(document.createElement("<input name=me type=radio>"));
}
0
 
LVL 7

Author Comment

by:brgivens
ID: 9717738
Oops... I included an external stylesheet & script in the last post... just remove them from the script.
0
 
LVL 12

Expert Comment

by:ahosang
ID: 9718064
Then you'll have to branch the function into Netscape and IE branches to cover for the IE bugs.
function test() {
  if (document.all) {//better in this case to specifically target IE in sniff
   // code that ran for IE
  } else {
    // code that ran for Netscape
  }
}
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

757 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

20 Experts available now in Live!

Get 1:1 Help Now