Solved

DOM Radio buttons in Netscape/Mozilla

Posted on 2003-11-10
6
1,448 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
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 how to count occurrences of each item in an array.
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…

910 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