Reputation: 2579
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.
Upvotes: 1
Views: 1796
Reputation: 2674
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;
}
Upvotes: 1
Reputation: 187110
Replace
var SelectedRow = Document.getElementById('selectedEventId');
with
var SelectedRow = document.getElementById(selectedEventId);
Since selectedEventId is a variable. In the first code piece it treats selectedEventId as string.
Note
Your ids should not start with numbers.
Upvotes: 1
Reputation: 5771
Should you be calling document.getElementById(selectedEventId) instead? (without quotes)
Upvotes: 0