Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show div when radio button selected

Tags:

I am novice in javascript and jQuery. In my html have 2 radio buttons and one div. I want to show that div if I check the first radio-button but otherwise I want it to be hidden

so: If radio button #watch-me is checked --> div #show-me is visible. If radio button #watch-me is unchecked (neither are checked or the second is checked) --> div #show-me is hidden.

Here is what I have so far.

 <form id='form-id'> <input id='watch-me' name='test' type='radio' /> Show Div<br /> <input name='test' type='radio' /><br /> <input name='test' type='radio' />  </form>  <div id='show-me' style='display:none'>Hello</div> 

and JS:

 $(document).ready(function () {  $("#watch-me").click(function() {  $("#show-me:hidden").show('slow');  });  $("#watch-me").click(function(){  if($('watch-me').prop('checked')===false) {     $('#show-me').hide();}     }); }); 

How should I change my script to achieve that?

like image 615
user2886091 Avatar asked Jan 16 '14 21:01

user2886091


People also ask

How do you show a div when a radio button is clicked in react?

You could just simply add a style declaration to your div you want to show, then just hook that up to your state object.

How do you show and hide input fields based on radio button selection?

To show or hide an element when a radio button is selected: Add a click event handler to all input elements of type radio . Each time a radio button is selected, check if it is the button that should show the element. If it is, set the display property of the hidden element to block .


1 Answers

I would handle it like so:

$(document).ready(function() {    $('input[type="radio"]').click(function() {        if($(this).attr('id') == 'watch-me') {             $('#show-me').show();                   }         else {             $('#show-me').hide();           }    }); }); 
like image 105
Eric J. Avatar answered Oct 25 '22 17:10

Eric J.