Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 774033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:03:49+00:00 2026-05-14T19:03:49+00:00

My question is to get onMouseover,onMouseout,onMousedown,onClick on a table row. For which i am

  • 0

My question is to get onMouseover,onMouseout,onMousedown,onClick on a table row. For which i am calling javascript userdefined functions.

onMouseover — Background color should change.
onMouseout — Reset to original color
onClick — First column checkbox/radio button should be set and background color should change
onMousedown — background color should change.

My code in html is:-
<tr onMouseOver="hover(this)" onMouseOut="hover_out(this)" onMouseDown="get_first_state(this)" onClick="checkit(this)" >

and the methods in javascripts are:-

var first_state = false;

var oldcol = ‘#ffffff’;
var oldcol_cellarray = new Array();

function hover(element) {

if (! element) element = this;
while (element.tagName != 'TR') {
    element = element.parentNode;
}

if (element.style.fontWeight != 'bold') {
    for (var i = 0; i<element.cells.length; i++) {
        if (element.cells[i].className != "no_hover") {
            oldcol_cellarray[i] = element.cells[i].style.backgroundColor;
            element.cells[i].style.backgroundColor='#e6f6f6';

        }
    }
}

}

// ———————————————————————————————–

function hover_out(element) {

if (! element) element = this;
while (element.tagName != 'TR') {
    element = element.parentNode;
}

if (element.style.fontWeight != 'bold') {
    for (var i = 0; i<element.cells.length; i++) {
        if (element.cells[i].className != "no_hover") {
            if (typeof oldcol_cellarray != undefined) {

                element.cells[i].style.backgroundColor=oldcol_cellarray[i];
            } else {
                element.cells[i].style.backgroundColor='#ffffff';

            }
        //var oldcol_cellarray = new Array();

        }
    }
}

}

// ———————————————————————————————–

function get_first_state(element) {

while (element.tagName != 'TR') {
    element = element.parentNode;
}
first_state = element.cells[0].firstChild.checked;

}

// ———————————————————————————————–

function checkit (element) {

while (element.tagName != 'TR') {
    element = element.parentNode;
}
if (element.cells[0].firstChild.type == 'radio') {
    var typ = 0;
} else if (element.cells[0].firstChild.type == 'checkbox') {
    typ = 1;
}
if (element.cells[0].firstChild.checked == true && typ == 1) {
    if (element.cells[0].firstChild.checked == first_state) {
        element.cells[0].firstChild.checked = false;
    }
    set_rowstyle(element, element.cells[0].firstChild.checked);
} else {
    if (typ == 0 || element.cells[0].firstChild.checked == first_state) {
        element.cells[0].firstChild.checked = true;

    }
    set_rowstyle(element, element.cells[0].firstChild.checked);
}
if (typ == 0) {
    var table = element.parentNode;
    if (table.tagName != "TABLE") {
        table = table.parentNode;
    }
    if (table.tagName == "TABLE") {
        table=table.tBodies[0].rows;
        //var table = document.getElementById("js_tb").tBodies[0].rows;
        for (var i = 1; i< table.length; i++) {
            if (table[i].cells[0].firstChild.checked == true && table[i] != element) {
                table[i].cells[0].firstChild.checked = false;
            }
            if (table[i].cells[0].firstChild.checked == false) {
                set_rowstyle(table[i], false);
            }
        }
    }
}

}

function set_rowstyle(r, on) {

if (on == true) {
    for (var i =0; i < r.cells.length; i++) {
        r.style.fontWeight = 'bold';
        r.cells[i].style.backgroundColor = '#f2f2c2';

    }
} else {
    for ( i =0; i < r.cells.length; i++) {
        r.style.fontWeight = 'normal';
        r.cells[i].style.backgroundColor = '#ffffff';
    }
}

}

It is working as expected in IE.

But coming to firefox i am surprised on seeing the output after so much of coding.

In Firefox:–
onMouseOver is working as expected. color change of that particular row.

onClick — Setting the background color permenantly..eventhough i do onmouseover on different rows. the clicked previous row color is not reset to white. — not as expected

onclick on 2 rows..the background of both the rows is set..Only the latest row color should be set. other rows that are selected before should be set back..not as expected i.e if i click on all the rows..background color of everything is changed…

Eventhough i click on the row. First column i.e radio button or checkbox is not set..

Please help me to solve this issue in firefox. Do let me know where my code needs to be changed…

Thanks in advance!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T19:03:50+00:00Added an answer on May 14, 2026 at 7:03 pm

    Sorry for making everything inline, but this should work in all browsers:

    <tr onmouseover="this.className += ' hover'" onmouseout="this.className = this.className.replace(/(^|\s)hover(\s|$)/,' ');" onclick="if(this.getElementsByTagName('input')[0].checked){this.className = this.className.replace(/(^|\s)click(\s|$)/,' ');this.getElementsByTagName('input')[0].checked = false;}else{this.className += ' click';this.getElementsByTagName('input')[0].checked = true;}">
    

    Here is a complete page you can test out:

    <html>
    <head>
    <style type="text/css">
    tr.click{
    background:yellow;
    }
    
    tr.hover{
    background:green;
    }
    </style>
    </head>
    <body>
    <table border="1">
    <tr onmouseover="this.className += ' hover'" onmouseout="this.className = this.className.replace(/(^|\s)hover(\s|$)/,' ');" onclick="if(this.getElementsByTagName('input')[0].checked){this.className = this.className.replace(/(^|\s)click(\s|$)/,' ');this.getElementsByTagName('input')[0].checked = false;}else{this.className += ' click';this.getElementsByTagName('input')[0].checked = true;}">
    <td>
    <input type="checkbox" readonly="readonly"/> click me
    </td>
    </tr>
    <tr onmouseover="this.className += ' hover'" onmouseout="this.className = this.className.replace(/(^|\s)hover(\s|$)/,' ');" onclick="if(this.getElementsByTagName('input')[0].checked){this.className = this.className.replace(/(^|\s)click(\s|$)/,' ');this.getElementsByTagName('input')[0].checked = false;}else{this.className += ' click';this.getElementsByTagName('input')[0].checked = true;}">
    <td>
    <input type="checkbox" readonly="readonly"/> click me
    </td>
    </tr>
    <tr onmouseover="this.className += ' hover'" onmouseout="this.className = this.className.replace(/(^|\s)hover(\s|$)/,' ');" onclick="if(this.getElementsByTagName('input')[0].checked){this.className = this.className.replace(/(^|\s)click(\s|$)/,' ');this.getElementsByTagName('input')[0].checked = false;}else{this.className += ' click';this.getElementsByTagName('input')[0].checked = true;}">
    <td>
    <input type="checkbox" readonly="readonly"/> click me
    </td>
    </tr>
    </table>
    </body>
    </html>
    

    I would strongly advise moving everything to an external JS file and using some sort of initialization function to assign the event listeners, instead of writing them all inline like me.

    I hope this helps in some way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

HTML Javascript question to get the selected value of a input-select I can use
im having trouble getting a popup using javascript to show table data onmouseover when
I was looking for an anwer to question Get next N elements from enumerable
From my question at Get Foreign Key Value , I managed to get the
I have a route defined like this: GET /question/:q_id controllers.Questions.viewQuestion(q_id: Long) Then in my
I'm using Alberto Santini's solution to this question to get a spiral grid reference
Question: I can get the SQL Server database language by querying: SELECT @@language And
QUESTION: how to get at the star above the numbers My FIDDLE currently does
Very basic question - how to get one value from a generator in Python?
Hopefully the last NN question you'll get from me this weekend, but here goes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.