Color table row if button clicked

I am looking to turn the whole row red in my table when clicked using javascript.

I have managed to turn single cells red with the below code but not the whole tr. Also once the row is red im unable to turn it back to white.

var ie = document.all
var ns = document.getElementById && !ie
function rowclick(e) {
var obj = ns ? e.target : event.srcElement
if (obj.tagName=="TD") {
obj.style.background="red"
obj.onblur=function() {
obj.style.background="white"
}
}
} 

Open in new window

Note that changing if
(obj.tagName=="TD") {
to
if (obj.tagName=="TR") {
does not work
runnerjp2005Asked:
Who is Participating?
 
Kyle AbrahamsSenior .Net DeveloperCommented:
fund a nice JS fiddle with JQUery . . . this uses a row click but can be changed to be triggered on the button click and the onblur for the exit:

source:
http://jsfiddle.net/QyZNQ/

$(document).ready(function () {
    $('tr').click(function () {
        if(this.style.background == "" || this.style.background =="white") {
            $(this).css('background', 'red');
        }
        else {
            $(this).css('background', 'white');
        }
    });
});

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.