Solved

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

Posted on 2016-09-07
4
50 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 (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 52

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 52

Expert Comment

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

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
The viewer will learn how to dynamically set the form action using jQuery.
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…

910 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

21 Experts available now in Live!

Get 1:1 Help Now