Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Jul 2012
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Something wrong with swtich statement?

    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


    Code:
    <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>
    Last edited by cris232; 07-04-2012 at 03:28 PM.

  • #2
    Regular Coder nomanic's Avatar
    Join Date
    Feb 2009
    Location
    United Kingdom
    Posts
    255
    Thanks
    9
    Thanked 33 Times in 33 Posts
    whats your question? how do you need help?
    <DmncAtrny> I will write on a huge cement block "BY ACCEPTING THIS BRICK THROUGH YOUR WINDOW, YOU ACCEPT IT AS IS AND AGREE TO MY DISCLAIMER OF ALL WARRANTIES, EXPRESS OR IMPLIED, AS WELL AS DISCLAIMERS OF ALL LIABILITY, DIRECT, INDIRECT, CONSEQUENTIAL OR INCIDENTAL, THAT MAY ARISE FROM THE INSTALLATION OF THIS BRICK INTO YOUR BUILDING."
    <DmncAtrny> And then hurl it through the window of a Sony officer
    <DmncAtrny> and run like hell

    Portfolio, Tutorials - http://www.nomanic.biz/

  • #3
    New Coder
    Join Date
    Jul 2012
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by nomanic View Post
    whats your question? how do you need help?
    my question is "Can anyone find something wrong with my codes"?

  • #4
    Regular Coder nomanic's Avatar
    Join Date
    Feb 2009
    Location
    United Kingdom
    Posts
    255
    Thanks
    9
    Thanked 33 Times in 33 Posts
    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
    <DmncAtrny> I will write on a huge cement block "BY ACCEPTING THIS BRICK THROUGH YOUR WINDOW, YOU ACCEPT IT AS IS AND AGREE TO MY DISCLAIMER OF ALL WARRANTIES, EXPRESS OR IMPLIED, AS WELL AS DISCLAIMERS OF ALL LIABILITY, DIRECT, INDIRECT, CONSEQUENTIAL OR INCIDENTAL, THAT MAY ARISE FROM THE INSTALLATION OF THIS BRICK INTO YOUR BUILDING."
    <DmncAtrny> And then hurl it through the window of a Sony officer
    <DmncAtrny> and run like hell

    Portfolio, Tutorials - http://www.nomanic.biz/

  • #5
    New Coder
    Join Date
    Jul 2012
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by nomanic View Post
    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?

  • #6
    Regular Coder nomanic's Avatar
    Join Date
    Feb 2009
    Location
    United Kingdom
    Posts
    255
    Thanks
    9
    Thanked 33 Times in 33 Posts
    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
    <DmncAtrny> I will write on a huge cement block "BY ACCEPTING THIS BRICK THROUGH YOUR WINDOW, YOU ACCEPT IT AS IS AND AGREE TO MY DISCLAIMER OF ALL WARRANTIES, EXPRESS OR IMPLIED, AS WELL AS DISCLAIMERS OF ALL LIABILITY, DIRECT, INDIRECT, CONSEQUENTIAL OR INCIDENTAL, THAT MAY ARISE FROM THE INSTALLATION OF THIS BRICK INTO YOUR BUILDING."
    <DmncAtrny> And then hurl it through the window of a Sony officer
    <DmncAtrny> and run like hell

    Portfolio, Tutorials - http://www.nomanic.biz/

  • #7
    New Coder
    Join Date
    Jul 2012
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by nomanic View Post
    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?

  • #8
    Regular Coder nomanic's Avatar
    Join Date
    Feb 2009
    Location
    United Kingdom
    Posts
    255
    Thanks
    9
    Thanked 33 Times in 33 Posts
    Code:
    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
    <DmncAtrny> I will write on a huge cement block "BY ACCEPTING THIS BRICK THROUGH YOUR WINDOW, YOU ACCEPT IT AS IS AND AGREE TO MY DISCLAIMER OF ALL WARRANTIES, EXPRESS OR IMPLIED, AS WELL AS DISCLAIMERS OF ALL LIABILITY, DIRECT, INDIRECT, CONSEQUENTIAL OR INCIDENTAL, THAT MAY ARISE FROM THE INSTALLATION OF THIS BRICK INTO YOUR BUILDING."
    <DmncAtrny> And then hurl it through the window of a Sony officer
    <DmncAtrny> and run like hell

    Portfolio, Tutorials - http://www.nomanic.biz/

  • #9
    New Coder
    Join Date
    Jul 2012
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by nomanic View Post
    Code:
    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

  • #10
    New Coder
    Join Date
    Jul 2012
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Code:
    <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
    Last edited by cris232; 07-04-2012 at 04:19 PM.


  •  

    Posting Permissions

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