Solved

DOM Radio buttons in Netscape/Mozilla

Posted on 2003-11-10
6
1,449 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Preface In the first article: A Better Website Login System (http://www.experts-exchange.com/A_2902.html) I introduced the EE Collaborative Login System and its intended purpose. In this article I will discuss some of the design consideratio…
This article discusses how to create an extensible mechanism for linked drop downs.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

777 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