Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
58
rated 0 times [  60] [ 2]  / answers: 1 / hits: 24170  / 13 Years ago, sun, august 28, 2011, 12:00:00

I have this piece of code:



<div class=quote-box-col14>
<label for=seo class=quote-service-seo>SEO</label>
<input type=checkbox name=seo id=seo value=Y class=checkbox-seo />
</div>


Someone can help with a JavaScript that adds class selected to when checkbox is selected?



Thanks


More From » jquery

 Answers
34

This jQuery will do it:



$('#seo').change(function(){
if($(this).is(:checked)) {
$(this).addClass(checked);
} else {
$(this).removeClass(checked);
}
});


See this jQuery fiddle for a working example.



Edit:



If you need it to work in IE too, use the click event instead:



$('#seo').click(function(){
if($(this).is(:checked)) {
$(this).addClass(checked);
} else {
$(this).removeClass(checked);
}
});

[#90379] Thursday, August 25, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
melindab

Total Points: 511
Total Questions: 109
Total Answers: 106

Location: San Marino
Member since Thu, Jun 25, 2020
4 Years ago
;