www.webdeveloper.com
Results 1 to 4 of 4

Thread: First Javascript

Hybrid View

  1. #1
    Join Date
    Mar 2013
    Posts
    14

    First Javascript

    Trying to make a simple javascript. I want to build a online test program. This is what I have but it apears to not work:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <script type="text/javascript" src="myfunction.js"></script>
    </head>
    <body>
    <div id="choices" >
    	<input type="radio" name="group1" onclick="myfunction();" value="Blue" />Blue <br/>
    	<input type="radio" name="group1" onclick="myfunction();" value="Red" />Red <br/>
    	<input type="radio" name="group1" onclick="myfunction();" value="Green" />Green <br/>
    	<input type="radio" name="group1" onclick="myfunction();" value="Yellow" />Yellow <br/>
    </div><br/>
    </body>
    </html>
    Code:
    <script type="text/javascript" >
     function myfunction()
        {
            var mycolor = document.getElementsByName("Group1");
    
            switch (mycolor) 
            {
                case "Blue":
    				alert("Blue");
    				break;
    				
    			case "Red":
    				alert("Red");
    				break;
    				
    			case "Green":
    				alert("Green");
    				break;
    				
    			case "Yellow":
    				alert("Yellow");
    				break;
            }
        }
    </script>

    This is just a test for the molment. I just need to capture the value the user clicks and use a switch to capture that value to see if they got it right or wrong.

  2. #2
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,671
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <script type="text/javascript">
    	function myfunction(obj)
        {
            var mycolor = obj.value;
    
            switch (mycolor) 
            {
                case "Blue":
    				alert("Blue");
    				break;
    				
    			case "Red":
    				alert("Red");
    				break;
    				
    			case "Green":
    				alert("Green");
    				break;
    				
    			case "Yellow":
    				alert("Yellow");
    				break;
            }
        }
    	</script>
    </head>
    <body>
    <div id="choices" >
    	<input type="radio" name="group1" onclick="myfunction(this);" value="Blue" />Blue <br/>
    	<input type="radio" name="group1" onclick="myfunction(this);" value="Red" />Red <br/>
    	<input type="radio" name="group1" onclick="myfunction(this);" value="Green" />Green <br/>
    	<input type="radio" name="group1" onclick="myfunction(this);" value="Yellow" />Yellow <br/>
    </div><br/>
    </body>
    </html>

  3. #3
    Join Date
    Mar 2013
    Posts
    14
    Thansk that worked but now im lost. What did you do that made it work? I mean I see you added "obj" but why did that fix it?

  4. #4
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,671
    compare not only the scripts but the markup too

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles