Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-27-2012, 01:10 AM   PM User | #1
codingcodeddode
New Coder

 
Join Date: Oct 2012
Posts: 18
Thanks: 1
Thanked 0 Times in 0 Posts
codingcodeddode is an unknown quantity at this point
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>
codingcodeddode is offline   Reply With Quote
Old 11-27-2012, 01:30 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 11-27-2012, 01:35 AM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
codingcodeddode (11-27-2012)
Old 11-27-2012, 01:37 AM   PM User | #4
codingcodeddode
New Coder

 
Join Date: Oct 2012
Posts: 18
Thanks: 1
Thanked 0 Times in 0 Posts
codingcodeddode is an unknown quantity at this point
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!
codingcodeddode is offline   Reply With Quote
Old 11-27-2012, 02:32 AM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 11-27-2012, 09:21 AM   PM User | #6
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Old 11-27-2012, 05:56 PM   PM User | #7
codingcodeddode
New Coder

 
Join Date: Oct 2012
Posts: 18
Thanks: 1
Thanked 0 Times in 0 Posts
codingcodeddode is an unknown quantity at this point
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!
codingcodeddode is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:50 PM.


Advertisement
Log in to turn off these ads.