Ronnie
Ronnie

Reputation: 13

hide-unhide with radio button

Im trying to unhide div id=Q3 with my first 2 radio inputs (id= Q1 & id = Q2)

if (Q1 = Ja and Q2 = Ja) show Q3.

Can somebody please help me!

See my html code below (sorry for the bad coding im a totally noob!).

HTML

<body>

<header>
    <h2>Helpdesk</h2>
    <div>Wat is u probleem?</div>
</header>

<form>

<h4>Kunt computer normaal aanzetten?<span class = "req" id= "req_Q1">*</span></h4>
<input type="radio" name="A1" id="A1" value ="Ja" /> : Ja<br />
<input type="radio" name="A1" id="A2" value ="Nee" /> : Nee<br />

<p class= "instruct" id="instruct_Q1">
<small>Controleer eerst de kabels!</small>
</p>


<h4 id= "Q2">Geeft de monitor normaal beeld?<span class = "req" id= "req_Q2">*</span> </h4>
<input type="radio" name="A2" id="A3" /> : Ja<br />
<input type="radio" name="A2" id="A4"/> : Nee<br />

<p class= "instruct" id="instruct_Q2">
<small>Controleer eerst de kabels!</small>
</p>
</div>

<div id= "Q3" style="display: none;">
<label for ="Q3.1" ><h4>Start windows normaal op?</h4></label>
<select name="Q3.1">
    <option value="A3.1" >Ja, geen probleem</option>
    <option value="A3.2" >Ja, maar met foutmelding</option>
    <option value="A3.3" >Nee, zwart scherm</option>
    <option value="A3.4" >Nee, blauw scherm</option>
</select>
</div>

<div style="display: none;"> 
<h4>Kunt u inloggen?</h4>
<input type="radio" name="Q4" />Ja, geen probleem<br />
<input type="radio" name="Q4" /> Nee, ik krijg een foutmelding<br />
</div>

<div style="display: none;"> 
<h4>Ik krijg de volgende foutmelding <span class = "req" id= "req_Q5">*</span></h4>
<input type="radio" name="Q5" />Kan u niet aanmelden bij het domein. Controleer of uw gebruikersnaam en/of toegangscode correct zijn.<br />
<input type="radio" name="Q5" />Kan u niet aanmelden omdat het domein niet beschikbaar is. <br />
<input type="radio" name="Q5" />Uw account is geblokkeerd. Neem contact op met de beheerder. <br />
</div>

Javascript

  $("input[name=A1]").click(function() 
  {
  if(this.id =="A1"){
    $("#Q3").show('slow');
  }
  else {
  $("#Q3").hide('slow');
  }
  });

Upvotes: 0

Views: 86

Answers (3)

StudioTime
StudioTime

Reputation: 24019

You can use a ternary instead of an if statement to make a little more elegant

/* get the status of the answers */
var status = $("#A1").is(':checked') && $("#A3").is(':checked');
/* let the status decide which $ method to use */
$("#Q3")[status ? 'fadeIn' : 'fadeOut'](500);

$("#helpdesk :input").change(function() {
  var status = $("#A1").is(':checked') && $("#A3").is(':checked');
  $("#Q3")[status ? 'fadeIn' : 'fadeOut'](500);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<header>
  <h2>Helpdesk</h2>
  <div>Wat is u probleem?</div>
</header>

<form id="helpdesk">

  <h4>Kunt computer normaal aanzetten?<span class="req" id="req_Q1">*</span></h4>
  <input type="radio" name="A1" id="A1" value="Ja" /> : Ja<br />
  <input type="radio" name="A1" id="A2" value="Nee" /> : Nee<br />

  <p class="instruct" id="instruct_Q1">
    <small>Controleer eerst de kabels!</small>
  </p>


  <h4 id="Q2">Geeft de monitor normaal beeld?<span class="req" id="req_Q2">*</span> </h4>
  <input type="radio" name="A2" id="A3" /> : Ja<br />
  <input type="radio" name="A2" id="A4" /> : Nee<br />

  <p class="instruct" id="instruct_Q2">
    <small>Controleer eerst de kabels!</small>
  </p>

  <div id="Q3" style="display: none;">
    <label for="Q3.1"><h4>Start windows normaal op?</h4></label>
    <select name="Q3.1">
    <option value="A3.1" >Ja, geen probleem</option>
    <option value="A3.2" >Ja, maar met foutmelding</option>
    <option value="A3.3" >Nee, zwart scherm</option>
    <option value="A3.4" >Nee, blauw scherm</option>
</select>
  </div>

  <div style="display: none;">
    <h4>Kunt u inloggen?</h4>
    <input type="radio" name="Q4" />Ja, geen probleem<br />
    <input type="radio" name="Q4" /> Nee, ik krijg een foutmelding<br />
  </div>

  <div style="display: none;">
    <h4>Ik krijg de volgende foutmelding <span class="req" id="req_Q5">*</span></h4>
    <input type="radio" name="Q5" />Kan u niet aanmelden bij het domein. Controleer of uw gebruikersnaam en/of toegangscode correct zijn.<br />
    <input type="radio" name="Q5" />Kan u niet aanmelden omdat het domein niet beschikbaar is. <br />
    <input type="radio" name="Q5" />Uw account is geblokkeerd. Neem contact op met de beheerder. <br />
  </div>

Upvotes: 1

Matthias Seifert
Matthias Seifert

Reputation: 2083

Look at this fiddle, and let me know, if it is the desired result :-) https://jsfiddle.net/7LL98L24/

$("#helpdesk :input").change(function() {
  if($("#A1").is(':checked') && $("#A3").is(':checked')) {
    $("#Q3").fadeIn(500);
  } else {
    $("#Q3").fadeOut(500);
  }
});

Please notice, that I added an ID to your form Element to use the .change Method of jQuery

Upvotes: 0

Luke_Nuke
Luke_Nuke

Reputation: 471

First of all to hide / unhide elements you should use the CSS. There was question about it on StackOverflow: JavaScript: How to Hide / Unhide <div>

However to my current knowledge to make IF statment - like you mentioned:

if(Q1 == Ja && Q2 == Ja){ show Q3 }

You need to use JavaScript.

EDIT: Hide/unhide div with button? I think there can be answer for your question: hide / unhide using JQuery - I found it preety quickly when asked google.

Upvotes: 0

Related Questions