Jquery to hightlight elements in a list
Posted
by John
on Stack Overflow
See other posts from Stack Overflow
or by John
Published on 2010-05-06T13:28:37Z
Indexed on
2010/05/06
13:38 UTC
Read the original article
Hit count: 261
Hi
I have a ol list:
<ol>
<li class="group1">item 1</li>
<li class="group1">item 2</li>
<li class="group2"> item 3</li>
<li class="group3">item 4</li>
<li class="group1">item 5</li>
<li class="group3"> item 6</li>
<ol>
and a set of checkboxes which correspond to the class names
<input type="checkbox" value="group1" />group 1
<input type="checkbox" value="group2" />group 2
<input type="checkbox" value="group3" />group 3
What I want to happen is that when a user clicks on a checkbox to 'tick' it, any li rows which are not checked are fadedOut (change opacity) and then any rows which have the class which matches the value of the checkbox are highlighter (background colour changed to yellow).
So for example if group 3 was clicked, item 4 and item 6 would be highlighted. Then if group 2 was clicked item 3 would be highlighted (item 4 and 6 would remain highlighted). If group 2 was un-ticked, item 3 would become faded out although item 4 and 6 would remain highlighted.
The code I have at the moment is:
$('input').click(function(){
input = $(this);
classVal = "." + input.val();
elements = $(classVal );
if (input.is(':checked')) {
elements.css("background-color", "#FFFF00");
} else {
elements.css("background-color", "");
}
});
This handles the highlighting but does not do the fading of the unchecked elements. I know I can change the opacity using css("opacity", 0.33) or fadeTo("slow", 0.33) but not sure how to handle this in the code and where to put it.
If any of my other code can be tidied up also please let me know
Thanks
© Stack Overflow or respective owner