Eternal J Eternal J - 5 months ago 17
PHP Question

Radio button text area

I have this survey form which works perfectly fine. I just need this one question to be changed. Basically there's 2 radio buttons for Answers "Yes" and "No" and a text area under them. Now I want the text area to be locked unless the user selects the "Yes" radio button then they can type in the text area a reason for "Yes".

I did some looking around and attempting this function but it doesn't seem to be working.



<script>
function validate() {
var radioYes = document.getElementById('radioYes');
var textarea = document.getElementById('question5comment');

if (radioYes.checked && question5comment.value.length < 1) {
alert('Please enter your reason for Question 5.');
question5comment.focus();
return false;
}
}

function toggle(value) {
document.getElementById('question5comment').disabled = value;
}
</script>

5. Are you using other non-franchise service centres?
<br>
*if yes is there any other reason you would do so other than price
<br>
<input type="radio" name="question5"
value="Yes" required>Yes
<br>
<input type="radio" name="question5"
<value="No" required>No
<br>
<textarea name="question5comment" rows="5" cols="40" required></textarea>




Answer

Your syntax is wrong:

  1. Unnecessary < before value.
  2. No ids that are referring in your code.
  3. Syntax errors.

Use the following:

function validate() {
  var radioYes = document.getElementById('radioYes');
  var textarea = document.getElementById('question5comment');

  if (radioYes.checked && question5comment.value.length < 1) {
    alert('Please enter your reason for Question 5.');
    question5comment.focus();
    document.getElementById('question5comment').disabled = true;
    return false;
  } else {
    document.getElementById('question5comment').disabled = false;
  }
}
5. Are you using other non-franchise service centres?
<br>*if yes is there any other reason you would do so other than price
<br>
<input type="radio" name="question5" value="Yes" required onclick="validate();" id="radioYes" />Yes
<br>
<input type="radio" name="question5" value="No" required onclick="validate();" id="radioNo" />No
<br>
<textarea name="question5comment" id="question5comment" rows="5" cols="40" required></textarea>