zigma
09-08-2003, 09:24 PM
Need help with Javascript mouseover here.
I just wondering if there is a cleaner way to write the following row highlight mouseover event
javascript code:
function highlight_on (thisobj,color)
{
thisobj.style.backgroundColor=color;
}
function highlight_off (thisobj,color)
{
thisobj.style.backgroundColor=color;
}
html file:
<table>
<tr onmouseover="highlight_on(this,'#EEF1FB')" onmouseout="highlight_off(this,'')">
<td>row 1</td>
</tr>
<tr onmouseover="highlight_on(this,'#EEF1FB')" onmouseout="highlight_off(this,'')">
<td>row 2</td>
</tr>
</table>
I really don't like to include all of those mouse event at each row. And if I decided to change the color of my whole site (Not just a page), there will be a lot of editing need to be done.
So I just wondering if anyone know if there a way to write it so I don't have to include all of those mouse event and specify it outside the tr tag in one global place/function?
Here is what I come up with but still doesn't work perfectly, need help with Javascript guru here.
Include at javascript file above:
window.onload = function()
{
// this is main fucntion to call on page load
init_table_list ('listing');
}
function init_table_list (tableid)
{
var tbid = document.getElementById(tableid);
tbid.onmouseover = function() {
highlight_on(this,'#EEF1FB')
}
tbid.onmouseout = function() {
highlight_off(this,'')
}
}
html file:
<table id="listing"> <!-- table id will be pass to init_table_list on load -->
<tr>
<td>row 1</td>
</tr>
<tr>
<td>row 2</td>
</tr>
</table>
The problem with this is the whole table get highlighted. I'm having trouble to get to it's tr tag to trigger mouseover.
It would work if I put the id=listing inside tr tag but that means I have to generate a unique id for all tr, which is not a good idea.
I also tried css hover. but it seems that css hover only work with <a href=> tag. So basiclly I tried to get the same result as I specify a:hover in one css file and include it at all page to have that mouseevent on every page. And I want to do the same thing with this row highlight thing.
This could be a simple problem for javascript guru, but I have to admit that I haven't write javascript that much to know how to fix this problem.
Any help is appreciated.
Thanks
I just wondering if there is a cleaner way to write the following row highlight mouseover event
javascript code:
function highlight_on (thisobj,color)
{
thisobj.style.backgroundColor=color;
}
function highlight_off (thisobj,color)
{
thisobj.style.backgroundColor=color;
}
html file:
<table>
<tr onmouseover="highlight_on(this,'#EEF1FB')" onmouseout="highlight_off(this,'')">
<td>row 1</td>
</tr>
<tr onmouseover="highlight_on(this,'#EEF1FB')" onmouseout="highlight_off(this,'')">
<td>row 2</td>
</tr>
</table>
I really don't like to include all of those mouse event at each row. And if I decided to change the color of my whole site (Not just a page), there will be a lot of editing need to be done.
So I just wondering if anyone know if there a way to write it so I don't have to include all of those mouse event and specify it outside the tr tag in one global place/function?
Here is what I come up with but still doesn't work perfectly, need help with Javascript guru here.
Include at javascript file above:
window.onload = function()
{
// this is main fucntion to call on page load
init_table_list ('listing');
}
function init_table_list (tableid)
{
var tbid = document.getElementById(tableid);
tbid.onmouseover = function() {
highlight_on(this,'#EEF1FB')
}
tbid.onmouseout = function() {
highlight_off(this,'')
}
}
html file:
<table id="listing"> <!-- table id will be pass to init_table_list on load -->
<tr>
<td>row 1</td>
</tr>
<tr>
<td>row 2</td>
</tr>
</table>
The problem with this is the whole table get highlighted. I'm having trouble to get to it's tr tag to trigger mouseover.
It would work if I put the id=listing inside tr tag but that means I have to generate a unique id for all tr, which is not a good idea.
I also tried css hover. but it seems that css hover only work with <a href=> tag. So basiclly I tried to get the same result as I specify a:hover in one css file and include it at all page to have that mouseevent on every page. And I want to do the same thing with this row highlight thing.
This could be a simple problem for javascript guru, but I have to admit that I haven't write javascript that much to know how to fix this problem.
Any help is appreciated.
Thanks