Click to See Complete Forum and Search --> : help needed


Odium
04-11-2003, 09:55 AM
Hi,

I just start learning javascript, so I try some easy functions. But I do not know why this one (below) do not work. PLZ help.




<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<script language="JavaScript1.3">
<!--
function vote(a)
{
var a = 0;
if(document.form1.bad==true)
{
a = 1;
alert(a);
}
else if(document.form1.bad==false)
{
a = 0;
alert(a);
}
//-->
</script>
</head>

<body bgcolor="#000033" text="#FFFFFF" link="#FFFFFF" vlink="#FFFFFF" alink="#FFFFFF" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<form name="form1">
<table width="80%" border="1" cellspacing="3" cellpadding="3">
<tr>
<td rowspan="2"><img src=""></td>
<td><input name="bad" type="radio">Schlecht</td>
<td><input name="bad" type="radio">Gut</td>
<td><input name="bad" type="radio">Geil</td>
<td><div align="center">
<input type="button" value="Abstimmen" onClick="vote(a)">
</div></td>
</tr>
</table>
</form>
</body>
</html>

khalidali63
04-11-2003, 10:00 AM
Your question is vague,what exactly is you are attempting to do and whats the error?


Cheers


Khalid

havik
04-11-2003, 10:57 AM
Exactly, I'm not sure what you're trying to do so this may or may not help.

First of all, your missing a }

Now, I'm gonna guess and say that you want to alert the number 1 if any radio button is selected, else you'll alert the number 0 if not. To do this, you don't need to have the variable a as a parameter.

Here is the code that'll do that:

function vote() {
var a = 0;
for(i = 0; i < document.form1.bad.length; i++) {
if(document.form1.bad[i].checked == true) {
a = 1;
}
}
alert(a);

}

and change the input button below to:
<input type="button" value="Abstimmen" onClick="vote()">

If this is not what you were looking for then oh well :)

Havik