/    Sign up×
Community /Pin to ProfileBookmark

CSS style based on checkbox checked/unchecked

I have a table with an indefinate number of rows set up similar to the following:

[CODE]<form name=”form1″>
<table>
<tr>
<td>name</td>
<td>check/uncheck all
<input type=”checkbox” name=”checkbox” value=”checkbox”> </td>
</tr>
<tr class=”normal”>
<td>Joe</td>
<td><input type=”checkbox” name=”checkbox0″ value=”checkbox”></td>
</tr>
<tr class=”normal”>
<td>Bob</td>
<td><input type=”checkbox” name=”checkbox1″ value=”checkbox”></td>
</tr>
</table>
</form>[/CODE]

The class “normal” is black text/white background
The class “inverted is white text/black background

What I am looking for is a way to change the class from “normal” to “inverted” when the checkbox within that row is checked. (when checked: class=”inverted” ; when unchecked: class=”normal”)

This could be done easily enough if the only way to check/uncheck a checkbox was to click on it. But I also use a javascript which checks/unchecks all checkboxes.

Is there a way to make the class reflect the value of the checkbox without having to click on the individual boxes?

Thank you ?

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@FangAug 23.2005 — <i>
</i>function check(obj) {
obj.parentNode.parentNode.className=(obj.checked)? 'inverted' : 'normal';
}
.
.
&lt;td&gt;&lt;input type="checkbox" name="checkbox0" value="checkbox" onchange="check(this)"&gt;&lt;/td&gt;
Copy linkTweet thisAlerts:
@julesfauthorAug 23.2005 — The document with suggested changes can be found at the following address:

http://julesf.com/test.html

The class is correctly changed when the individual boxes are checked, but using the checkall function does not create an onChange event.

Any suggestions? Thanks ?
Copy linkTweet thisAlerts:
@julesfauthorAug 23.2005 — I finally got it:

[CODE]var checkflag = "false";
function checkall(field) {
if (checkflag == "false") {
for (i = 1; i < field.length; i++) {
field[i].checked = true;
check(field[i]);}
checkflag = "true";
return "Uncheck All"; }
else {
for (i = 1; i < field.length; i++) {
field[i].checked = false;
check(field[i]);}
checkflag = "false";
return "Check All"; }
}
function check(obj) {
obj.parentNode.parentNode.className=(obj.checked)? 'inverted' : 'normal';
}[/CODE]
×

Success!

Help @julesf 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.5,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...