Nemanja Kezic
Nemanja Kezic

Reputation: 17

How to fill textbox using radio button

I have a code with button Click. When i click text appears in textbox. It woks. But i have a problem to write anoter type of code. I want to fill the textbox using radio buttons but without button Click. So when I click in a radio I want to show text in textbox. Here is my code.

<script type="text/javascript">
  function testResults (form) {
    var TestVar1 = form.input[0].checked;
    var TestVar2 = form.input[1].checked;
    var TestVar3 = form.input[2].checked;
    var TestVar4 = form.input[3].checked;
    if (TestVar1 == true) {
      form.textbox.value = "Earth is...";
    } else if (TestVar2 == true){
      form.textbox.value = "Mars is...";
    } else if (TestVar3 == true){
      form.textbox.value = "Jupiter is...";
    } else if (TestVar4 == true){
      form.textbox.value = "Saturn is..."; 
    }
  }
</script>
</head>
<body>
    <center>
    <h1>Our Universe</h1>
    </center>
    <form>
        Select Planet<br />
        <input type="radio" name="input" onclick='check_value(0)'/>Earth<p>
        <input type="radio" name="input" onclick='check_value(1)'/>Mars<p>
        <input type="radio" name="input" onclick='check_value(2)'/>Jupiter<p>
        <input type="radio" name="input" onclick='check_value(3)'/>Saturn<p>
        <input type="button" name="button" value="Click" onClick="testResults(this.form)"><p>
        <textarea name="comments" id="textbox" rows="5" cols="50"></textarea>
    </form>
</body>

Upvotes: 0

Views: 1795

Answers (2)

user2755150
user2755150

Reputation:

To do that on clicking radio butttons a way could be this:

<input type="radio" name="input" onclick="check_value(this.value)" value="Earth" />Earth<p>
<input type="radio" name="input" onclick="check_value(this.value)" value="Mars"/>Mars<p>
<input type="radio" name="input" onclick="check_value(this.value)" value="Jupiter"/>Jupiter<p>
<input type="radio" name="input" onclick="check_value(this.value)" value="Saturn"/>Saturn<p>

and js:

function check_value(txt){
    document.form.textbox.value = txt + " is...";
}

Upvotes: 2

Diogo Cid
Diogo Cid

Reputation: 3794

See if is this you want.

<script type="text/javascript"> function check_value (value,form) { if (value == 0) { form.textbox.value = "Earth is..."; } else if (value == 1){ form.textbox.value = "Mars is..."; } else if (value == 2){ form.textbox.value = "Jupiter is..."; } else if (value == 3){ form.textbox.value = "Saturn is..."; } } </script> </head> <body> <center> <h1>Our Universe</h1> </center> <form> Select Planet<br /> <input type="radio" name="input" onclick='check_value(0,this.form)'/>Earth<p> <input type="radio" name="input" onclick='check_value(1,this.form)'/>Mars<p> <input type="radio" name="input" onclick='check_value(2,this.form)'/>Jupiter<p> <input type="radio" name="input" onclick='check_value(3,this.form)'/>Saturn<p> <input type="button" name="button" value="Click" onClick="testResults(this.form)"><p> <textarea name="comments" id="textbox" rows="5" cols="50"></textarea> </form> </body>

Upvotes: 1

Related Questions