jquery - Change background color of div when checkbox is clicked -


i have form several checkboxes, this:

<div><input type='checkbox' name='groupid' value='1'>1</div> <div><input type='checkbox' name='groupid' value='2'>2</div> <div><input type='checkbox' name='groupid' value='3'>3</div> 

what i'm trying when checkbox checked, change background color of div, , when it's unchecked, remove background color. how can using jquery?

thanks

jquery:

$("input[type='checkbox']").change(function(){     if($(this).is(":checked")){         $(this).parent().addclass("redbackground");      }else{         $(this).parent().removeclass("redbackground");       } }); 

css:

.redbackground{     background-color: red;   } 

i'd recommend using add/remove class, opposed changing css of parent div directly.

demo: http://jsfiddle.net/ujcb7/


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -