CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Sep 2008
    Posts
    3

    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>
    Last edited by PeejAvery; September 20th, 2008 at 12:05 PM. Reason: Added code tags.

  2. #2
    Join Date
    May 2002
    Posts
    10,943

    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)"
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  3. #3
    Join Date
    Sep 2008
    Posts
    3

    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.

  4. #4
    Join Date
    Sep 2008
    Posts
    3

    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.

  5. #5
    Join Date
    May 2002
    Posts
    10,943

    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.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  6. #6
    Join Date
    Dec 2007
    Posts
    5

    Re: javascript switch case statement.

    Use FireBug - it's a great firefox addon

  7. #7
    Join Date
    Oct 2008
    Posts
    1

    Re: javascript switch case statement.

    onmouseover="alert(getValue(1));"

  8. #8
    Join Date
    Jul 2005
    Location
    Currently in Mexico City
    Posts
    568

    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...
    Wanna install linux on a vacuum cleaner. Could anyone tell me which distro sucks better?

    I had a nightmare last night. I was dreaming that I’m 64-bit and my blanket is 32-bit and I couldn’t cover myself with it, so I’ve spent the whole night freezing. And in the morning I find that my blanket just had fallen off the bed. =S (from: bash.org.ru)

    //always looking for job opportunities in AU/NZ/US/CA/Europe :P
    willCodeForFood(Arrays.asList("Java","PHP","C++","bash","Assembler","XML","XHTML","CSS","JS","PL/SQL"));

    USE [code] TAGS! Read this FAQ if you are new here. If this post was helpful, please rate it!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured