Solved

Use jQuery to show/hide a div when checkbox is checked

Posted on 2009-05-16
1
3,266 Views
Last Modified: 2012-05-07
Hi there!

I'm looking for a short and sweet jQuery snippet that will show a div based on it's ID when a certain checkbox is checked. Then if the checkbox is not checked, the div will be invisible...

I know this is probably pretty easy and I've seen it before, but I can't find where I've seen it now...

Below is the full section of the form that I'm dealing with at the moment. The checkbox with the ID "bgimg-yes" is the controller... If it is checked, then I want the div with the ID of "bgimgs" to be visible. If it's NOT checked, then I want the div ID "bgimgs" to be invisible.
<fieldset>

			<legend>Look &amp; Feel</legend>

			<span class="sexy_option">Use a background image? <input <?php echo (($sexy_plugopts['bgimg-yes'] == "yes")? 'checked="checked"' : ""); ?> name="bgimg-yes" id="bgimg-yes" type="checkbox" value="yes" /></span>

			<div id="bgimgs">

			<label class="bgimg share-sexy"><input <?php echo (($sexy_plugopts['bgimg'] == "sexy")? 'checked="checked"' : ""); ?> id="bgimg-sexy" name="bgimg" type="radio" value="sexy" /></label>

			<label class="bgimg share-care"><input <?php echo (($sexy_plugopts['bgimg'] == "caring")? 'checked="checked"' : ""); ?> id="bgimg-caring" name="bgimg" type="radio" value="caring" /></label>

			<label class="bgimg share-wealth"><input <?php echo (($sexy_plugopts['bgimg'] == "wealth")? 'checked="checked"' : ""); ?> id="bgimg-wealth" name="bgimg" type="radio" value="wealth" /></label>

			<!--<label class="bgimg"><input <?php echo (($sexy_plugopts['bgimg'] == "none")? 'checked="checked"' : ""); ?> id="bgimg-none" name="bgimg" type="radio" value="none" />(none)</label>-->

			</div>

			<div class="clear"></div>

						

			<span class="sexy_option">Animate-expand multi-lined bookmarks?</span>

			<label><input <?php echo (($sexy_plugopts['expand'] == "1")? 'checked="checked"' : ""); ?> name="expand" id="expand-yes" type="radio" value="1" />Yes</label>

			<label><input <?php echo (($sexy_plugopts['expand'] != "1")? 'checked="checked"' : ""); ?> name="expand" id="expand-no" type="radio" value="0" />No</label>

			

			<span class="sexy_option">Auto-center the bookmarks?</span>

			<label><input <?php echo (($sexy_plugopts['autocenter'] == "1")? 'checked="checked"' : ""); ?> name="autocenter" id="autocenter-yes" type="radio" value="1" />Yes</label>

			<label><input <?php echo (($sexy_plugopts['autocenter'] != "1")? 'checked="checked"' : ""); ?> name="autocenter" id="autocenter-no" type="radio" value="0" />No</label>

			<p>(Note: selecting <b>Yes</b> above will void any custom styles applied below.)</p>

				

			

			<label for="xtrastyle">You can style the DIV that holds the menu here:</label><br/>

			<textarea id="xtrastyle" name="xtrastyle">

<?php 

	$default_sexy = "margin:20px 0 0 0 !important;\npadding:25px 0 0 10px !important;\nheight:29px;/*the height of the icons (29px)*/\ndisplay:block !important;\nclear:both !important;";

	if (!empty($sexy_plugopts['xtrastyle'])) {

		echo $sexy_plugopts['xtrastyle']; 

	} 

	elseif ( empty($sexy_plugopts['xtrastyle']) )

		echo $default_sexy;

	else {

		echo "If you see this message, please delete the contents of this textarea and click \"Save Changes\".";

	}

?>

			</textarea>
 

		</fieldset>

Open in new window

0
Comment
Question by:blatantwaste
1 Comment
 
LVL 3

Accepted Solution

by:
blatantwaste earned 0 total points
Comment Utility
Nevermind, I figured it out on my own...

Here's how I did it.

First, I added the class "hidden" to the div so it looked like:

<div id="bgimgs" class="hidden">

In the stylesheet the class hidden was set to "display:none;"

Then in my script, I used the following:
jQuery(document).ready(function() {

  jQuery('#bgimg-yes').click(function() {

   if (this.checked) {

    this.checked=jQuery('#bgimgs').removeClass('hidden');

   } else {

      jQuery('#bgimgs').addClass('hidden');

     }

  });

}

Open in new window

0

Featured Post

IT, Stop Being Called Into Every Meeting

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

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
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…
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)

772 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

16 Experts available now in Live!

Get 1:1 Help Now