By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,453 Members | 3,194 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,453 IT Pros & Developers. It's quick & easy.

change background color on hover or select

P: n/a
I have a table with a form consisting of several checkboxes and I'm
wondering if its possible to change the table row background color on
mouseover or hover and make it stay that color when the checkbox in that row
is selected?
Poko
Sep 25 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a
Gary said the following on 9/25/2005 3:18 AM:
I have a table with a form consisting of several checkboxes and I'm
wondering if its possible to change the table row background color on
mouseover or hover and make it stay that color when the checkbox in that row
is selected?


Yes, it is very possible and quite simple actually. Search the archives,
give it your best try, and post back with some code and/or a URL to your
efforts. Or, did you expect someone to write the entire thing from scratch?

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Sep 25 '05 #2

P: n/a
Randy Webb wrote:
Gary said the following on 9/25/2005 3:18 AM:
I have a table with a form consisting of several checkboxes and I'm
wondering if its possible to change the table row background color on
mouseover or hover and make it stay that color when the checkbox in
that row is selected?


Yes, it is very possible and quite simple actually. Search the
archives, give it your best try, and post back with some code and/or
a URL to your efforts. Or, did you expect someone to write the entire
thing from scratch?


:) Point taken, thank you for your response Randy
poko
Sep 25 '05 #3

P: n/a
ASM
Gary a écrit :
I have a table with a form consisting of several checkboxes and I'm
wondering if its possible to change the table row background color on
mouseover or hover and make it stay that color when the checkbox in that row
is selected?
Poko


No, you have a form containing a table
(table can't contain a form)
(table can contain elements of a form)

see that :
http://perso.wanadoo.fr/stephane.mor...ht_rows_en.htm
<html><form><table border=1 cellspacing=0>
<tr>
<td>
<input type=checkbox onclick="highLght(this)">
</td>
<td>blah</td><td>blah</td><td>blah</td>
</tr>
<tr>
<td>
<input type=checkbox onclick="highLght(this)">
</td>
<td>blah</td><td>blah</td><td>blah</td>
</tr>
</table></form>
<script type="text/javascript">
function highLght(chckbox) {
var rw = chckbox.parentNode.parentNode;
rw.className = (chckbox.checked)? 'backCol' : '';
}
</script>
<style type="text/css">
..backCol { background-color: yellow }
</style>
</html>
--
Stephane Moriaux et son [moins] vieux Mac
Sep 25 '05 #4

P: n/a
ASM
Randy Webb a écrit :
Or, did you expect someone to write the entire thing from scratch?


Sorry ...

--
Stephane Moriaux et son [moins] vieux Mac
Sep 25 '05 #5

P: n/a
ASM wrote:
Gary a écrit :
I have a table with a form consisting of several checkboxes and I'm
wondering if its possible to change the table row background color on
mouseover or hover and make it stay that color when the checkbox in
that row is selected?
Poko
No, you have a form containing a table
(table can't contain a form)
(table can contain elements of a form)


:) u da man!
But of course you are right.

see that :
http://perso.wanadoo.fr/stephane.mor...ht_rows_en.htm merci beaucoup ASM, I'm afraid to say its slightly above my abilities but it
gives me something to work with :)
poko

<html><form><table border=1 cellspacing=0>
<tr>
<td>
<input type=checkbox onclick="highLght(this)">
</td>
<td>blah</td><td>blah</td><td>blah</td>
</tr>
<tr>
<td>
<input type=checkbox onclick="highLght(this)">
</td>
<td>blah</td><td>blah</td><td>blah</td>
</tr>
</table></form>
<script type="text/javascript">
function highLght(chckbox) {
var rw = chckbox.parentNode.parentNode;
rw.className = (chckbox.checked)? 'backCol' : '';
}
</script>
<style type="text/css">
.backCol { background-color: yellow }
</style>
</html>

Sep 25 '05 #6

P: n/a
ASM wrote on 25 sep 2005 in comp.lang.javascript:
Gary a écrit :
I have a table with a form consisting of several checkboxes and I'm
wondering if its possible to change the table row background color on
mouseover or hover and make it stay that color when the checkbox in
that row is selected?
Poko
No, you have a form containing a table
(table can't contain a form)
(table can contain elements of a form)

see that :
http://perso.wanadoo.fr/stephane.mor...hlight_rows_en.
htm
<html><form><table border=1 cellspacing=0>

[..] </html>


This will hover and will be clickable along the whole <tr>:

<html>

<style type="text/css">
..backCol { background-color:yellow;}
..hoverbackCol { background-color:blue;color:white;}
</style>

<script type="text/javascript">

function ifhover(x,istrue) {
if (istrue) bgsave=x.className
x.className = (istrue)?'hoverbackCol':
(x.firstChild.firstChild.checked)? 'backCol' : '';
}

function clickhighLght(x) {
x.firstChild.firstChild.checked =
!x.firstChild.firstChild.checked;
x.className =
(x.firstChild.firstChild.checked)? 'backCol' : '';
}

function highLght(chckbox) {
clickhighLght(chckbox.parentNode.parentNode);
}
</script>

<form>
<table border=1 cellspacing=0>

<tr onclick="clickhighLght(this)"
onmouseover="ifhover(this,true)"
onmouseout="ifhover(this, false)">
<td>
<input type=checkbox onclick="highLght(this)">
</td>
<td>blah</td><td>blah</td><td>blah</td>
</tr>

<tr onclick="clickhighLght(this)"
onmouseover="ifhover(this,true)"
onmouseout="ifhover(this, false)">
<td>
<input type=checkbox onclick="highLght(this)">
</td>
<td>blah</td><td>blah</td><td>blah</td>
</tr>

</table>
</form>
</html>

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Sep 25 '05 #7

P: n/a
Evertjan. wrote:
ASM wrote on 25 sep 2005 in comp.lang.javascript:
Gary a écrit :
I have a table with a form consisting of several checkboxes and I'm
wondering if its possible to change the table row background color
on mouseover or hover and make it stay that color when the checkbox
in that row is selected?
Poko


No, you have a form containing a table
(table can't contain a form)
(table can contain elements of a form)

see that :
http://perso.wanadoo.fr/stephane.mor...hlight_rows_en.
htm
<html><form><table border=1 cellspacing=0>

[..]
</html>


This will hover and will be clickable along the whole <tr>:

<html>

<style type="text/css">
.backCol { background-color:yellow;}
.hoverbackCol { background-color:blue;color:white;}
</style>

<script type="text/javascript">

function ifhover(x,istrue) {
if (istrue) bgsave=x.className
x.className = (istrue)?'hoverbackCol':
(x.firstChild.firstChild.checked)? 'backCol' : '';
}

function clickhighLght(x) {
x.firstChild.firstChild.checked =
!x.firstChild.firstChild.checked;
x.className =
(x.firstChild.firstChild.checked)? 'backCol' : '';
}

function highLght(chckbox) {
clickhighLght(chckbox.parentNode.parentNode);
}
</script>

<form>
<table border=1 cellspacing=0>

<tr onclick="clickhighLght(this)"
onmouseover="ifhover(this,true)"
onmouseout="ifhover(this, false)">
<td>
<input type=checkbox onclick="highLght(this)">
</td>
<td>blah</td><td>blah</td><td>blah</td>
</tr>

<tr onclick="clickhighLght(this)"
onmouseover="ifhover(this,true)"
onmouseout="ifhover(this, false)">
<td>
<input type=checkbox onclick="highLght(this)">
</td>
<td>blah</td><td>blah</td><td>blah</td>
</tr>

</table>
</form>
</html>


Evertjan!
thats exactly what I was hoping for......thank you!
poko
Sep 25 '05 #8

P: n/a
ASM wrote in message news:43***********************@news.wanadoo.fr...
Gary a écrit :
I have a table with a form consisting of several checkboxes and I'm
wondering if its possible to change the table row background color on
mouseover or hover and make it stay that color when the checkbox in that row
is selected?
Poko
see that :
http://perso.wanadoo.fr/stephane.mor...ht_rows_en.htm
A very long long time ago (about 4 years) I managed to put together the
following "tables" and javascript (sorry about all the junk in there):
http://www.geocities.com/peg_seti/gr...tm#country-tbl
I have no idea on what browsers it worked, but it still does in FF ;-)
The idea was to be able to move the mouse over a row in any of the last
four tables and it would highlight the respective row in the other three.

I did validate it once , but as I said, that was eons of years ago.
So ignore the align="absbottom" errors if you /have/ to validate it ;-)
Thank FrontPage of Micro$oft for that...

BTW, you say
No, you have a form containing a table
(table can't contain a form)
actually it can; not directly in the <table> tag but in <TD> and <TH>

<!ENTITY % block
"P | %heading; | %list; | %preformatted; | DL | DIV | CENTER |
NOSCRIPT | NOFRAMES | BLOCKQUOTE | FORM | ISINDEX | HR |
TABLE | FIELDSET | ADDRESS">
---> ^^^^^ ^^^^

<!ENTITY % flow "%block; | %inline;">
---> ^^^^^^^
<!ELEMENT TABLE - -
(CAPTION?, (COL*|COLGROUP*), THEAD?, TFOOT?, TBODY+)>

<!ELEMENT (TH|TD) - O (%flow;)* -- table header cell, table data cell-->
---> ^^^^^^
(table can contain elements of a form)


I have both in http://www.geocities.com/peg_seti

<table>
<tr>
<td>
<form ...>
<table>
<tr>
<td>
<input ...>
</td>
</tr>
</table>
</form>
</td>
</tr>
</table>

Sep 25 '05 #9

P: n/a
ASM
Robi a écrit :
ASM wrote in message news:43***********************@news.wanadoo.fr...
see that :
http://perso.wanadoo.fr/stephane.mor...ht_rows_en.htm

A very long long time ago (about 4 years)


Do you mean I did nothing invent ?
You're right.
Sometimes, I try some idea found here or there
and I keep it to show to others if I think it can be of some interest.
(any copyright, even not my name most of the time)
I managed to put together the
following "tables" and javascript (sorry about all the junk in there):
http://www.geocities.com/peg_seti/gr...tm#country-tbl
I have no idea on what browsers it worked, but it still does in FF ;-)
Oh, I have also a little demo about that, not so elaborated than yours.
The most difficulty is to crutch IE
The idea was to be able to move the mouse over a row in any of the last
four tables and it would highlight the respective row in the other three.
For the moment I'm fighting with appendChild in a table a cloneNoded row.
That's work fine with FF, Opera and others except ... IE (of course)
I have yet to do a simplier test before coming to ask here what is wrong.
I did validate it once
Hope you will not try with my little demos ...
I didn't it.
No, you have a form containing a table
(table can't contain a form)

actually it can; not directly in the <table> tag but in <TD> and <TH>


That's what I wanted to say, in fact the form is in a td
by chance a td is part of a table :-)
(because certainly you use a table as structural tool)

By end and ... finaly the form contains a table
turned this way, I could too say the table below contains a table :-)
<table>
<tr>
<td>
<form ...>
<table>
<tr>
<td>
<input ...>
</td>
</tr>
</table>
</form>
</td>
</tr>
</table>

--
Stephane Moriaux et son [moins] vieux Mac
Sep 25 '05 #10

P: n/a
ASM wrote:
[...]

For the moment I'm fighting with appendChild in a table a cloneNoded row.
That's work fine with FF, Opera and others except ... IE (of course)
I have yet to do a simplier test before coming to ask here what is wrong.


Remember that when encountering a table, browsers will insert a tbody
element into the DOM even if there isn't one in the source HTML (they
are mandatory elements but the tags are optional).

A common problem is attempting to append a new row to a table element
when you should append to a tbody element. Other browsers will tolerate
it and add the row to the tbody anyway, but not IE.

e.g. if 'el' is a reference to a table:

var aRow = el.rows[0].cloneNode(true);
el.appendChild(aRow);

will not work in IE. But:

var aRow = el.rows[0].cloneNode(true);
var lastRow = el.rows[el.rows.length - 1];
lastRow.parentNode.insertBefore(aRow, lastRow.nextSibling);

will work (and in others too) because the parent of the lastRow is a
tbody, not a table.

There is a sample below. Of course I could have just used a tbody
element instead of the table when climbing the tree, this is just a demo.

<script type="text/javascript">

function cloneRow( el )
{
// Get the table - should use tbody but this is just for show
while (el.parentNode && 'table' != el.nodeName.toLowerCase()){
el = el.parentNode;
}
var aRow = el.rows[0].cloneNode(true);

// The next line won't work in IE
// el.appendChild(aRow);

// But these will
var lastRow = el.rows[el.rows.length - 1];
lastRow.parentNode.insertBefore(aRow, lastRow.nextSibling);

}

</script>

<table border="1">
<tr>
<td onclick="cloneRow(this)">Click me</td>
</tr>
</table>

[...]

--
Rob
Sep 25 '05 #11

P: n/a
ASM
RobG a écrit :
ASM wrote:
[...]

For the moment I'm fighting with appendChild in a table a cloneNoded row.
That's work fine with FF, Opera and others except ... IE (of course)
I have yet to do a simplier test before coming to ask here what is wrong.

Remember that when encountering a table, browsers will insert a tbody
element into the DOM even if there isn't one in the source HTML (they
are mandatory elements but the tags are optional).

A common problem is attempting to append a new row to a table element
when you should append to a tbody element. Other browsers will tolerate
it and add the row to the tbody anyway, but not IE.


No, no, I appendChild in a tbody
But, perhaps my row is too heavy for this poor IE (my IE5.2 Mac ...)
with css and IE's roll-over +
image (with onclick) +
input with onchange
etc

way I do :
I have an undisplayed table with one row
I have an other table of x rows (thead, tbody, tfoot)
each row of each tbody of both tables are same (except a td text)

var rw = document.getElementById('refRow').cloneNode(true);
give a new id to the clone
var tb = document.getElementById('tBodyTarget');
tb.appendchild(rw);
original row being not undisplayed
I have not to display in IE understanding : tableRow

IE is lost with width of cols (1st col width becomes initial table width)
IE forget all events (an alert showes it)

I did try with M$ function : insertRow()
without succes because that would only create a new empty row
(and I'm not sure that works with IE Mac)
e.g. if 'el' is a reference to a table:

var aRow = el.rows[0].cloneNode(true);
el.appendChild(aRow);

will not work in IE. But:

var aRow = el.rows[0].cloneNode(true);
var lastRow = el.rows[el.rows.length - 1];
lastRow.parentNode.insertBefore(aRow, lastRow.nextSibling);
Perhaps could I try that
(but I have a tfoot ... ?)
will work (and in others too) because the parent of the lastRow is a
tbody, not a table.

There is a sample below. Of course I could have just used a tbody
element instead of the table when climbing the tree, this is just a demo.
keep it here for my archives (and later try)
<script type="text/javascript">

function cloneRow( el )
{
// Get the table - should use tbody but this is just for show
while (el.parentNode && 'table' != el.nodeName.toLowerCase()){
el = el.parentNode;
}
var aRow = el.rows[0].cloneNode(true);

// The next line won't work in IE
// el.appendChild(aRow);

// But these will
var lastRow = el.rows[el.rows.length - 1];
lastRow.parentNode.insertBefore(aRow, lastRow.nextSibling);

}

</script>

<table border="1">
<tr>
<td onclick="cloneRow(this)">Click me</td>
</tr>
</table>

--
Stephane Moriaux et son [moins] vieux Mac
Sep 26 '05 #12

P: n/a
ASM wrote:
RobG a écrit :
ASM wrote:
[...]

For the moment I'm fighting with appendChild in a table a cloneNoded
row.
That's work fine with FF, Opera and others except ... IE (of course)
I have yet to do a simplier test before coming to ask here what is
wrong.


Remember that when encountering a table, browsers will insert a tbody
element into the DOM even if there isn't one in the source HTML (they
are mandatory elements but the tags are optional).

A common problem is attempting to append a new row to a table element
when you should append to a tbody element. Other browsers will
tolerate it and add the row to the tbody anyway, but not IE.

No, no, I appendChild in a tbody
But, perhaps my row is too heavy for this poor IE (my IE5.2 Mac ...)


Hmm, it was a long shot. I'll have a play with IE Mac later (actually
much later... say +7hrs from now).

[...]
--
Rob
Sep 26 '05 #13

P: n/a
ASM
RobG a écrit :
ASM wrote:
RobG a écrit :
ASM wrote:
[...]
For the moment I'm fighting with appendChild in a table a cloneNoded
row.
That's work fine with FF, Opera and others except ... IE (of course)

A common problem is attempting to append a new row to a table element
when you should append to a tbody element. Other browsers will
tolerate it and add the row to the tbody anyway, but not IE.


No, no, I appendChild in a tbody
But, perhaps my row is too heavy for this poor IE (my IE5.2 Mac ...)


Hmm, it was a long shot. I'll have a play with IE Mac later (actually
much later... say +7hrs from now).


if ...
http//perso.wanadoo.fr/stephane.moriaux/truc/clone_row.htm
--
Stephane Moriaux et son [moins] vieux Mac
Sep 26 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.