Solved

DOM Radio buttons in Netscape/Mozilla

Posted on 2003-11-10
6
1,450 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

828 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