?
Solved

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

Posted on 2016-09-07
4
Medium Priority
?
130 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 44

Expert Comment

by:zephyr_hex (Megan)
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 59

Accepted Solution

by:
Julian Hansen earned 2000 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 59

Expert Comment

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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to dynamically set the form action using jQuery.
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)
Suggested Courses

718 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