×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Radio Button Clicked, Another Radio Becomes Mandatory

Radio Button Clicked, Another Radio Becomes Mandatory

Radio Button Clicked, Another Radio Becomes Mandatory

(OP)
Hi guys, I've got the code below.

The first line 'Yes' or 'No' is mandatory. When 'Yes' is clicked, the 2nd line (No, Probably or Yes) becomes mandatory. If 'No' is clicked on the first line, the 2nd line is not mandatory.

I've been trying and I can't get it to work as I want it to. Any help is appreciated. Thanks guys!


CODE

<script>
function myTest() {		
		 document.getElementById("fruity").required = true;
		 document.getElementById("fruityJuice").required = true;
		 }
</script>

<form id="form1" name="form1" method="POST" action="test.html">
<!-- question 1 below is mandatory-->
Yes  <input name="fruity" id="fruity" type="radio" value="Yes" /> 
No   <input name="fruity" id="fruity" type="radio" value="No" />
<p>

<!-- question 2 below is mandatory if 'Yes' was clicked in question 1. if 'No', not mandatory   -->
No <input name="fruityJuice" id="fruityJuice" type="radio" value="No" />  
Probably <input name="fruityJuice" id="fruityJuice" type="radio" value="Probably" />  
Yes   <input name="fruityJuice" id="fruityJuice" type="radio" value="Yes" />

<input onclick="return myTest();" type="submit" name="Submit" value="Submit" />
</form> 

RE: Radio Button Clicked, Another Radio Becomes Mandatory

Hi

Please note that
  • The id attributes must be unique per document.
  • If the form is submitted without clicking the Submit button, then button's onclick event may or may not be triggered, depending on the browser.
So I would do it like this :

CODE

<script>
addEventListener('load', handle_question_1_change)

function handle_question_1_change()
{
  document.querySelectorAll('form#form1 input[name="fruity"]').forEach(function(input) {
    input.addEventListener('change', change_question_2_requiredness)
  })
}

function change_question_2_requiredness(event)
{
  document.querySelectorAll('form#form1 input[name="fruityJuice"]').forEach(function(input) {
    input.required = event.target.value == 'Yes'
  })
}
</script>

<form id="form1" name="form1" method="POST" action="test.html">
<!-- question 1 below is mandatory-->
Yes  <input name="fruity" type="radio" value="Yes" required="required" /> 
No   <input name="fruity" type="radio" value="No" required="required" />
<p>

<!-- question 2 below is mandatory if 'Yes' was clicked in question 1. if 'No', not mandatory   -->
No <input name="fruityJuice" type="radio" value="No" />  
Probably <input name="fruityJuice" type="radio" value="Probably" />  
Yes   <input name="fruityJuice" type="radio" value="Yes" />

<input type="submit" name="Submit" value="Submit" />
</form> 

See it in action on JSFiddle : https://jsfiddle.net/0c3jva2k/

Feherke.
feherke.github.io

RE: Radio Button Clicked, Another Radio Becomes Mandatory

(OP)
Wow! Thanks very much Feherke! That's exactly what I needed. Have a very good day!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close