change background color on hover or select

G

Gary

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
 
R

Randy Webb

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?
 
P

Poko

Randy said:
Gary said the following on 9/25/2005 3:18 AM:

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
 
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.moriaux/truc/tables_highlight_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>
 
E

Evertjan.

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.moriaux/truc/tables_highlight_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>
 
P

Poko

Evertjan. said:
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.moriaux/truc/tables_highlight_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
 
R

Robi

ASM wrote in message
Gary a écrit :

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/graphs.htm#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+)>

(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>
 
A

ASM

Robi a écrit :
ASM wrote in message


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/graphs.htm#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.
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 :)
 
R

RobG

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>

[...]
 
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>
 
R

RobG

ASM said:
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).

[...]
 
A

ASM

RobG a écrit :
ASM said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top