?
Solved

DOM Radio buttons in Netscape/Mozilla

Posted on 2003-11-10
6
Medium Priority
?
1,453 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 200 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

This article discusses how to implement server side field validation and display customized error messages to the client.
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

765 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