Solved

How can I show or hide div with radio button and jQuery, and process on page load?

Posted on 2016-09-07
4
37 Views
Last Modified: 2016-09-07
I am using the below code to show or hide a div with jQuery. It works fine on the first visit, but if a person hits the back button and visits the page while the "ShowIt" radio button is selected, it will be hidden, because it's hidden by default. How can i make it show if the ShowIt radio is selected when the page loads? Thank you!

	$(document).ready(function(){
		$('#myDiv').hide();
	    $('input[type="radio"]').click(function(){
	        if($(this).attr("value")=="HideIt"){
	            $('#myDiv').hide('fast');
	        }
	        if($(this).attr("value")=="ShowIt"){
	            $('#myDiv').show('fast');
	        }	
	    });
	});

Open in new window

0
Comment
Question by:earwig75
  • 2
4 Comments
 
LVL 42

Expert Comment

by:zephyr_hex
ID: 41788316
You can look for the checked status of the radio and show the div if it's checked:

if ($('#ShowIt').is(':checked')) {
  $('#myDiv').show();
}

Open in new window


Here is a Fiddle Demo.
0
 
LVL 51

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 41788382
Another option
if ($('input:checked').val() == 'ShowIt') {
  $('#myDiv').show('fast');
}

Open in new window

Here is the complete code - I am assuming you have given your radio's a name attribute to group them - I have used that in the code to target the click as well
<script>
$(function() {
  if ($('input:checked').val() == 'ShowIt') {
    $('#myDiv').show('fast');
  }
  
  $('input[name="toggle"]').click(function() {
  if ($(this).val() == 'ShowIt') {
    $('#myDiv').show();
  }
  else {
    $('#myDiv').hide();
  }
  });
});
</script>

Open in new window

Working sample here

EDIT:
HTML used for above sample
    <input type="radio" name="toggle" value="ShowIt" /> Show <input type="radio" name="toggle" value="HideIt" /> Hide <br/>
    <div id="myDiv" style="display: none" >This is my div</div>

Open in new window

0
 

Author Closing Comment

by:earwig75
ID: 41788446
Ignore my last comment, this works perfectly, thank you.
0
 
LVL 51

Expert Comment

by:Julian Hansen
ID: 41788452
You are welcome.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now