views:

28

answers:

3

I am trying to select a table row, and highlight it with javascript. However, nothing seems to work. When I comment out the first two lines of the following code, I can highlight, but when I click another row, the previously selected rows stay highlighted instead of going back to white.

    var selectedEventId = 0;

    function SelectRow(tableRow){
       var SelectedRow = Document.getElementById('selectedEventId');
       SelectedRow.style.backgroundColor = 'white';

       var frame = document.getElementById('additionalText');
       frame.src="iframeContents.php?id="+tableRow.id;
       selectedEventId = tableRow.id;
       tableRow.style.backgroundColor = '3366ff';
       var prevRow = document.getElementById('selectedEventId');
       return;

    }//end SelectRow

Any help would be appreciated.

A: 

Should you be calling document.getElementById(selectedEventId) instead? (without quotes)

Roy Tang
+1  A: 
rahul
+1  A: 

Try this out:


var selectedEventId = 0; 
var prevRow = ''; 
function SelectRow(tableRow){ 
    if (prevRow != '') { prevRow.backgroundColor = 'white'; }
    var frame = document.getElementById('additionalText'); 
    frame.src="iframeContents.php?id="+tableRow.id; 
    selectedEventId = tableRow.id; 
    tableRow.style.backgroundColor = '3366ff';
    // based on the assumption that selectedEventId is set as a global variable
    prevRow = document.getElementById(selectedEventId);
    return; 
}
drlouie - louierd
YAY!! This works, except, in the if statement, I had to do prevRow.style.backgroundColor = 'white'; Thanks a LOT!!!!!
Sakamoto Kazuma