Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Show And Hide Input Fields Based On Radio Button Selection

Here is the code to show input fields depending on radio selection like:

SCRIPT

<script type="text/javascript">  function yesnoCheck() {     if (document.getElementById('yesCheck').checked) {         document.getElementById('ifYes').style.visibility = 'visible';     } else {         document.getElementById('ifYes').style.visibility = 'hidden';     }  </script> 

HTML

Yes <input type="radio" onclick="javascript:yesnoCheck();" name="yesno" id="yesCheck"/>No <input type="radio" onclick="javascript:yesnoCheck();" name="yesno" id="noCheck"/> <br> <div id="ifYes" style="visibility:hidden">If yes, explain:     <input type='text' id='yes' name='yes'/>     <br>What can we do to accommodate you?         <input type='text' id='acc' name='acc'/> </div>    other 3 <input type='text' id='other3' name='other3'> <br>     other 4 <input type='text' id='other4' name='other4'> <br> 

However I would like input fields to be hidden (like on the image), and make they do not use any space until radio button is selected, when radio is selected show them with fade effect...

Here is the fiddle:

enter image description here

like image 553
edgarmtze Avatar asked Jul 12 '13 18:07

edgarmtze


People also ask

How do you show and hide div elements based on the selection of radio buttons?

Answer: Use the jQuery show() and hide() methods You can simply use the jQuery show() and hide() methods to show and hide the div elements based on the selection of radio buttons. The div boxes in the following example are hidden by default using the CSS display property which value is set to none .


1 Answers

Replace all instances of visibility style to display

display:none //to hide display:block //to show 

Here's updated jsfiddle: http://jsfiddle.net/QAaHP/16/

You can do it using Mootools or jQuery functions to slide up/down but if you don't need animation effect it's probably too much for what you need.

CSS display is a faster and simpler approach.

like image 74
Learner Avatar answered Sep 24 '22 00:09

Learner