Solved

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

Posted on 2016-09-07
4
104 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 43

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 58

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 58

Expert Comment

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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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 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…
Suggested Courses

628 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