javascript switch case statement.
I'm new to the switch statement and I'm trying to put the case one value in "onmouseover="getValue(1)"" and can't seem to get it to work. Could someone tell me what I'm doing wrong
Also I'll have several cases because this is going to be a yahtzee game.
Code:
function getValue(category)
{
d = new Array(6);
for(var i = 0; i < 5; i++)
{
// parses values from numbered pictures and plases them into another // array for sorting
d[i] = 1 * dice[i].src.charAt(dice[i].src.length - 5);
}
d.sort();
switch(category)
{
case 1:
{
// Counts the ones in d[]
for(var i = 1; i < 6; i++)
{
if(d[i] == 1)
{
ones++;
}
}
return ones;
}
break;
}
}
//html
<tr>
<td><label id="Label1">Ones</label></td>
<td><input id="category" name="Category1" type="text" nmouseover = "getValue(1)" /></td>
</tr>
Re: javascript switch case statement.
1. There are no {} after case. Take them out and your code should work.
2. For valid HTML there should be no spaces in your HTML tags between attributes and values.
Code:
onmouseover="getValue(1)"
3. Rather than use onmouseover, why don't you use onchange so you don't have to rely on the mouse?
Code:
onchange="getValue(1)"
Re: javascript switch case statement.
What I wanted to do is allow the user to see their possible score before clicking. Later I will put an onmousclick method in there to lock the score in.
Re: javascript switch case statement.
Code:
function getValue(category)
{
d = new Array(6);
for(var i = 0; i < 5; i++)
{
d[i] = 1 * dice[i].src.charAt(dice[i].src.length - 5);
}
d.sort();
switch(category)
{
case 1:
for(var i = 1; i < 6; i++)
{
if(d[i] == 1)
{
category++;
}
}
return category;
break;
}
}
Is there a syntax error. Its not working.
Re: javascript switch case statement.
I don't see any syntax error. However, if you want to check for errors, you can do it yourself. Use Firefox's Error Console from the Tools menu.
Re: javascript switch case statement.
Use FireBug - it's a great firefox addon
Re: javascript switch case statement.
onmouseover="alert(getValue(1));"
Re: javascript switch case statement.
What you probably want is this:
HTML Code:
<script>
function showHelp(id,valId,category,e){
if(e.pageY){
document.getElementById(id).style.top = (e.pageY+25)+"px";
document.getElementById(id).style.left = (e.pageX-10)+"px";
}else{
document.getElementById(id).style.top = (e.clientY+25)+"px";
document.getElementById(id).style.left = (e.clientX-10)+"px";
}
//document.getElementById(valId).innerHTML = getValue(category);
document.getElementById(valId).innerHTML = "8";
document.getElementById(id).style.display = "block";
}
function hideHelp(id){
document.getElementById(id).style.display = "none";
}
function getValue(category){
var ones = 0;
var d = new Array(6);
for(var i=0; i<5; i++){
d[i] = 1 * dice[i].src.charAt(dice[i].src.length - 5);
}
d.sort();
switch(category){
case 1:
for(var i=1; i<6; i++){
if(d[i] == 1){ones++;}
}
break;
default:
break;
}
return ones;
}
</script>
<table>
<tr>
<td><label id="Label1">Ones</label></td>
<td><input id="category" name="Category1" type="text" onmousemove="showHelp('divH','cVal',1,event);" onmouseout="hideHelp('divH');" style="cursor:help;" /></td>
</tr>
</table>
<div id="divH" style="border:2px solid #999999; background-color:#FFFFFF; width:150px; height:120px; text-align:center; display:none; position:absolute;">
<p>some help text</p><p>value: <span id="cVal" style="color:#FF0000;"></span></p>
</div>
I've corrected the getvalue() function, though since I don't have your dice images I can't simulate the whole process or check if the logic is correct. I do not think that this would be a problem for you though...