gsaldutti Posted October 17, 2012 Share Posted October 17, 2012 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); }); Quote Link to comment https://forums.phpfreaks.com/topic/269599-jquery-changing-background-color-upon-clicking-div/ Share on other sites More sharing options...
Jessica Posted October 17, 2012 Share Posted October 17, 2012 You'll have to either check the current color before changing it, or store the current color in a variable and check that before changing it. Quote Link to comment https://forums.phpfreaks.com/topic/269599-jquery-changing-background-color-upon-clicking-div/#findComment-1385876 Share on other sites More sharing options...
galvin Posted October 18, 2012 Share Posted October 18, 2012 Ok, I'm going to change the class of the clickable DIV upon clicking and change the color that way. Thanks for pointing me in the right direction! Quote Link to comment https://forums.phpfreaks.com/topic/269599-jquery-changing-background-color-upon-clicking-div/#findComment-1385902 Share on other sites More sharing options...
codefossa Posted October 18, 2012 Share Posted October 18, 2012 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> Quote Link to comment https://forums.phpfreaks.com/topic/269599-jquery-changing-background-color-upon-clicking-div/#findComment-1385904 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.