Jump to content

Recommended Posts

I am using this code below to check/uncheck a checkbox that is inside a DIV, upon clicking anywhere on the DIV...

$("#checkbox").live('click', function(){
	 var $tc = $(this).find('input:checkbox:first'),
		 tv = $tc.attr('checked');

	 $tc.attr('checked', !tv);
 });

 

I now would like to also have the background color change upon clicking anywhere on the DIV. For example, if they click the DIV, the checkbox would become checked AND the background of the DIV would become green. Then if clicked again, the checkbox would become unchecked and the background would go back to white.

 

I got this far, and it does change the background to green upon clicking, but I can't figure out the part for "removing of background color". Any ideas who to accomplish that part?

 

$("#checkbox").live('click', function(){
 var $bg = $(this).css("background-color","green");
	 var $tc = $(this).find('input:checkbox:first'),
		 tv = $tc.attr('checked');

	 $tc.attr('checked', !tv);
 });

Here's a quickly thrown together way.

http://xaotique.no-ip.org/tmp/7/

 

You just add a checkbox inside a div with the class "check" and it will do it. I showed with 5 just to show you can have a bunch of'em.

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
   <head>
       <title>Xaotique</title>
       <meta http-equiv="content-type" content="text/html;charset=utf-8" />
       <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
       <script type="text/javascript">
           $(document).ready(function()
           {
               $(".check input").attr("ischecked", false);
               $(".check input").attr("checked", false);

               $(".check").click(function()
               {
                   var checked = $(this).find("input:first").attr("ischecked") == "true" ? false : true;
                   var color = checked ? "green" : "red";

                   $(this).find("input:first")
                       .attr("checked", checked)
                       .attr("ischecked", checked);

                   $(this).css("background-color", color);
               });
           });
       </script>
   </head>

   <body>
       <?php for ($i = 0; $i < 5; $i++) { ?>
           <div class="check" style="width: 100px; height: 20px; background-color: red; float: left; margin-right: 3px;">
               <input type="checkbox" /> Click Me
           </div>
           <br /><br />
       <?php } ?>
   </body>
</html>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.