...

View Full Version : Something wrong with swtich statement?



cris232
07-04-2012, 03:24 PM
need help:
if you click on Name and then submit it will display this: Your name is Anonymous
click on GF: You have no GF
click on Home:You live in America
click on gender: You are a male
default : You are weak
you can click multiple check box and display said clicked checkboxes
i used switch statements and this shows up
http://images.4chan.org/g/src/1341400993245.jpg


<html>
<title>ACT</title>

<head>
<center><h3>PERSONAL INFORMATION</h3></center>
<hr>
</head>
<center><body>
<table width="300px">
<tr>
<td><input type="checkbox" value="LEVE" id="info"/>NAME</td>
</tr>
<tr>
<td><input type="checkbox" value="GF" id="info"/>GF</td>
</tr>
<tr>
<td><input type="checkbox" value="America" id="info"/>HOME</td>
</tr>
<tr>
<td><input type="checkbox" value="Male" id="info"/>GENDER</td>
</tr>
<tr>
<td><br>
</td>
</tr>
<tr>
<td><button type="button" onclick="result()">Click Me</button>
</td>
</tr>
<tr>
<td>
<p id="name"></p>
<p id="GF"></p>
<p id="Tagum"></p>
<p id="Male"></p>
<p id="break"></p>
</td>
</tr>


</table>
<script type="text/javascript">
var a=document.getElementById("info").value;

var b;
var c;
var d;
var f;
var e;


function result()
{
switch (a)
{
case "LEVE":
b="Your name is Anonymous";
break;
case "GF":
c="You have no GF";
break;
case "America":
d="You live in America";
break;
case "Male":
f="You are a male";
break;
default:
e="You weak";

}
document.getElementById("name").innerHTML=b;
document.getElementById("GF").innerHTML=c;
document.getElementById("Tagum").innerHTML=d;
document.getElementById("Male").innerHTML=f;
document.getElementById("break").innerHTML=e;
}

</script>


</body></center>


</html>

nomanic
07-04-2012, 03:52 PM
whats your question? how do you need help?

cris232
07-04-2012, 03:55 PM
whats your question? how do you need help?

my question is "Can anyone find something wrong with my codes"?

nomanic
07-04-2012, 03:55 PM
This code wont work

For starters you have 4 input boxes with id="info", each should have a seperate id
you cant just have variable a as value of info, it will only ever capture first input box, or nothing in ie
you dont need a switch statement for this, you need to capture each input box seperately and test for each in turn

cris232
07-04-2012, 03:58 PM
This code wont work

For starters you have 4 input boxes with id="info", each should have a seperate id
you cant just have variable a as value of info, it will only ever capture first input box, or nothing in ie
you dont need a switch statement for this, you need to capture each input box seperately and test for each in turn

So i need to have a variable for every input box?

nomanic
07-04-2012, 03:59 PM
call them info0, info1, info2, etc
then instead of one capture and switch statement
capture each in turn
test for checked on each
and set output as necessary

cris232
07-04-2012, 04:01 PM
call them info0, info1, info2, etc
then instead of one capture and switch statement
capture each in turn
test for checked on each
and set output as necessary
how do you "check" each input box?

nomanic
07-04-2012, 04:05 PM
a=document.getElementById('info0');
if (a.checked) {
document.getElementById("name").innerHTML="Your name is Anonymous";
}
a=document.getElementById('info1');
if (a.checked) {
document.getElementById("GF").innerHTML="You have no GF";
}
etc etc

cris232
07-04-2012, 04:07 PM
a=document.getElementById('info0');
if (a.checked) {
document.getElementById("name").innerHTML="Your name is Anonymous";
}
a=document.getElementById('info1');
if (a.checked) {
document.getElementById("GF").innerHTML="You have no GF";
}
etc etc


hmm.. thank you, will try it now

cris232
07-04-2012, 04:16 PM
<html>
<title>ACT</title>

<head>
<center><h3>PERSONAL INFORMATION</h3></center>
<hr>
</head>
<center><body>
<table width="300px">
<tr>
<td><input type="radio" value="Your name is Anonymous" id="name"/>NAME</td>
</tr>
<tr>
<td><input type="radio" value="You have no GF" id="gf"/>GF</td>
</tr>
<tr>
<td><input type="radio" value="You live in America" id="home"/>HOME</td>
</tr>
<tr>
<td><input type="radio" value="You are a male" id="gender"/>GENDER</td>
</tr>
<tr>
<td><br>
</td>
</tr>
<tr>
<td><button type="button" onclick="result()">Click Me</button>
</td>
</tr>
<tr>
<td>
<p id="name2"></p>
<p id="GF"></p>
<p id="Tagum"></p>
<p id="Male"></p>

</td>
</tr>


</table>
<script type="text/javascript">


function result()
{
var n;
n=document.getElementById("name");
if (n.checked)
{
document.getElementById("name2").innerHTML="Your name is Anonymous";
}
n=document.getElementById("gf");
if (n.checked)
{
document.getElementById("GF").innerHTML="You have no GF";
}
n=document.getElementById("home");
if (n.checked)
{
document.getElementById("Tagum").innerHTML="You live in America";
}
n=document.getElementById("gender");
if (n.checked)
{
document.getElementById("Male").innerHTML="You are a male";
}



}


</script>


</body></center>


</html>


finally it worked, thank you very much, kinda new to the javascript environment



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum