PDA

View Full Version : variable as a form element name?



htcilt
Sep 14th, 2007, 04:45 PM
Is it possible to use variables as the name for a form element e.g.


<script type="text/Javascript">
<html>
<head>
<script type="text/Javascript">
var myvar = 1;
</script>
</head>
<body>
<input name="myvar" type="radio" value="radiobutton" />test
</body>
</html>



I tested the above and looked at the course. The name is shown as "myvar" and not "1", so I guess it didn't work.

nikkiH
Sep 14th, 2007, 05:20 PM
Not like that you can't.
(this should be posted in the javascript forum)

Are you sure you want to do that? You'll completely break the form for anyone who can't use javascript.

That said, if you're going to do something that breaks it for non-script browsers, create the whole element dynamically so that it is not even there for those people. Elements must be in a form. They need to be named properly. Not a number.


<!doctype html public "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title> New Document </title>
<script type="text/javascript">
function createElement(eName)
{
if (document.getElementById && document.createElement)
{
document.getElementById("form1").appendChild(document.createElement("<input type='text' name='"+eName+"'>"));
}
}
</script>
</head>

<body onLoad="createElement('txt1')">
<form name="form1">
</form>
</body>
</html>

htcilt
Sep 14th, 2007, 05:50 PM
thats great, thanks.
p.s. sorry about posting in the wrong forum.

nikkiH
Sep 14th, 2007, 08:17 PM
You're welcome. No worries.