/    Sign up×
Community /Pin to ProfileBookmark

Jquery checkbox and background color

Hi,

I want to change the background color of li element to be changed when clicked. So when the user clicks on first li, the background color should be in red, and when he clicks second one, the second one should change to red, but the first one should go to white.

In short, the background color of selected li should be changed to show it is highlighted.

Also the checkbox should also be checked for only selected li.
I have tried to do like below. But it is changing for all selected li’s..
Please help.

<html>
<head>
<script type=”text/javascript” src=”http://editor.webyana.com/javascripts/client_scripts/potential/jquery.js”></script>
<script>
$(document).ready(function(){
$(“li.gc”).click(function(){

$(“li.gc”).each(function(i)
{
$(“li.gc[i]“).css({backgroundColor:’#FFA700′});

});
$(this).css({backgroundColor: ‘#000000’});

$(this).children(“input[type=checkbox]”).attr(‘checked’, ‘checked’);

//do something with the sortcat
});

});

</script>
</head>
<body>
<ul>
<li class=”gc”>

<input type=”checkbox”/>Check1
<a href=”#”>Link1</a>

</li>
<li class=”gc”>
<div>

<input type=”checkbox”/>Check2
<a href=”#”>Link1</a>
</div>
</li>
<li class=”gc”>

<div>
<input type=”checkbox”/>Check3
<a href=”#”>Link1</a>
</div>
</li>
<li class=”gc”>
<div>

<input type=”checkbox”/>Check4
<a href=”#”>Link1</a>
</div>
</li>
</ul>
</body>
</html>

to post a comment
JavaScript

0Be the first to comment 😎

×

Success!

Help @Manasipradeep spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...