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 7 of 7
  1. #1
    New Coder
    Join Date
    Oct 2012
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Radio Buttons and Switch Statements HELP

    How could I get a string to be sent to the textbox depending on which radio button is clicked? MUST BE RADIO BUTTONS AND MUST USE SWITCH STRUCTURE

    heres my code.

    Code:
    <body>
     <form id='form'>
    	<input type='radio' name='week' value='sunday' id='sunday' onclick='runme();'>Sunday<br>
    	<input type='radio' name='week' value='monday' id='monday' onclick='runme();'>Monday<br>
    	<input type='radio' name='week' value='tuesday' id='tuesday' onclick='runme();'>Tuesday<br>
    	<input type='radio' name='week' value='wednssday' id='wednesday' onclick='runme();'>Wednesday<br>
    	<input type='radio' name='week' value='thursday' id='thursday' onclick='runme();'>Thursday<br>
    	<input type='radio' name='week' value='friday' id='friday' onclick='runme();'>Friday<br>
    	<input type='radio' name='week' value='saturday' id='saturday' onclick='runme();'>Saturday<br>
    	<input value='This is ' id='textBox'>
    </form>
     </body>

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,700
    Thanks
    80
    Thanked 4,658 Times in 4,620 Posts
    Well, you already have an onclick there on each radio button, so what do YOU think the code in the runme function might look like?

    I have to say that s switch is ALMOST the worst possible choice for this. I have to assume it is yet another idiotic homework assignment given you by yet another incompetent instructor. No?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,700
    Thanks
    80
    Thanked 4,658 Times in 4,620 Posts
    I know you can't do it this way, but this is the simplest and most efficient:
    Code:
    <body>
     <form id='form'>
    	<input type='radio' name='week' value='Sunday'>Sunday<br>
    	<input type='radio' name='week' value='Monday'>Monday<br>
    	<input type='radio' name='week' value='Tuesday'>Tuesday<br>
    	<input type='radio' name='week' value='Wednesday' >Wednesday<br>
    	<input type='radio' name='week' value='Thursday' >Thursday<br>
    	<input type='radio' name='week' value='Friday' >Friday<br>
    	<input type='radio' name='week' value='Saturday' >Saturday<br>
    	<input name="weekday" >
    </form>
    
    <script type="text/javascript">
    (
      function( )
      {
          var f = document.getElementById("form");
    
          var rbs = f.week;
          for ( var r = 0; r < rbs.length; ++r )
          {
              rbs[r].onclick = getWeekday;
          }
          function getWeekday( )
          {
              f.weekday.value = "You chose " + this.value;
          }
      }
    )();
    </script>
    </body>
    </html>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    codingcodeddode (11-27-2012)

  • #4
    New Coder
    Join Date
    Oct 2012
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    Well, you already have an onclick there on each radio button, so what do YOU think the code in the runme function might look like?

    I have to say that s switch is ALMOST the worst possible choice for this. I have to assume it is yet another idiotic homework assignment given you by yet another incompetent instructor. No?
    Thats what i thought, I had already done this very easily with an if statement, but, yes sadly less funding leads to the employment of unfit instructors. There are several students who seem to know more than the instructor lol!

    THANK GOODNESS FOR FORMS AND PEOPLE LIKE YOU!

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,700
    Thanks
    80
    Thanked 4,658 Times in 4,620 Posts
    As ugly as it is, I think you may have to do something like this:
    Code:
    	<input type='radio' name='week' value='sunday' onclick='runme(this);'>Sunday<br
    and then your runme will have to do something like:
    Code:
    function runme( rb )
    {
        var rbValue = rb.value;
        var message;
        switch( rbValue )
        {
            "sunday": message = "You chose Sunday"; break;
            ... etc. ...
        }
        ... put message into text field ...
    }
    It's so stupid. A useless usage of switch. But if that's what doofus wants, that's what doofus gets.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,316
    Thanks
    203
    Thanked 2,566 Times in 2,544 Posts
    Is there any other subject in your school - law, medicine, math, French, whatever, where the teachers are incompetent and the students are more knowledgeable? Why is it imagined that any doofus (presumany a failure in some other field) can teach computer programming?

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #7
    New Coder
    Join Date
    Oct 2012
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    Is there any other subject in your school - law, medicine, math, French, whatever, where the teachers are incompetent and the students are more knowledgeable? Why is it imagined that any doofus (presumany a failure in some other field) can teach computer programming?
    Well i haven't noticed any other instructors as terrible as this guy, but then again I haven't been attending long enough to notice. All my other instructors are, i don't want to say great, but their not terrible.

    How I admire this nations education system!


  •  

    Posting Permissions

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