How to highlight an item in a table...please help

A

almurph

Hi,

I'm a newbie to Javascript. I can;t even finf a full API for it!
Anyway I have a table structure and when a user presses the down-arrow
button the item in said table is meant to be highlighted.

Here is the code that I have so far:

document.getElementById('tableID').style.backgroundColor ="#000000";
document.getElementById('tableID').setAttribute ("border", 2);
document.getElementById('tableID').setAttribute(1, 1)

Ques: How do I make an item in the list become highlighted?

Thanking you,
Al.
 
A

ASM

(e-mail address removed) a écrit :
when a user presses the down-arrow
button the item in said table is meant to be highlighted.

Here is the code that I have so far:

document.getElementById('tableID').style.backgroundColor ="#000000";
document.getElementById('tableID').setAttribute ("border", 2);
document.getElementById('tableID').setAttribute(1, 1)

with down arrow

function hglg(e) {
var t = document.getElementById('tableID');
t.style.background='#ffc';
t.style.color='red';
t.style.border='1px solid black';
if(e.keyCode == '40') {
t.style.background='#000';
t.style.color='white';
t.style.border='2px solid yellow';
}
}

<body onkeypress="hglg(event);">


Ques: How do I make an item in the list become highlighted?


the easiest way is to have a reserved css class for hightlighting

CSS :
=====
table { width: 100% }
..hl { background: #000; border-width: 2px; color: white }
td { background: #ffc; border: 1px solid orange; color: maroon;
text-align: center; cursor: pointer;}

JS :
====
// to get collection of TDs in the page
onload = function() {
T = document.getElementsByTagName('TD');
// to give onclick at each TD
for(var i=0; i<T.length; i++) T.onclick = function(){ hl(this);};
}

function hl(what) { // hightlighter function
// all TDs without class
for(var i=0; i<T.length; i++) T.className = '';
// 'what' or this TD with class 'hl'
what.className = 'hl';
}

HTML:
=====
<table>
<tr><td>test 1</td><td>test 2</td><td>test 3</td></tr>
<tr><td>test 4</td><td>test 5</td><td>test 6</td></tr>
</table>


for a newbie :

<html>
<table border=2 width="100%">
<tr>
<td onclick="var s = this.style;
s.background='#000'; s.color='white';">
test 1
</td>
<td onclick="var s = this.style;
s.background='#000'; s.color='white';">
test 2
</td>
<td onclick="var s = this.style;
s.background='#000'; s.color='white';">
test 3
</td>
</tr>
<tr>
<td onclick="var s = this.style;
s.background='#000'; s.color='white';">
test 4
</td>
<td onclick="var s = this.style;
s.background='#000'; s.color='white';">
test 5
</td>
<td onclick="var s = this.style;
s.background='#000'; s.color='white';">
test 6
</td>
</tr>
</html>
 
W

wisestpotato

Anyway I have a table structure and when a user presses the down-arrow
button the item in said table is meant to be highlighted.

Try the following javascript. This will start off by highlighting the
first row in the table. You can then move the highlight up and down
using the up and down arrow keys.

var currentRowIndex = 0; // current highlighted row
var colourHighlight = "#c8fa63"; // background colour of
highlighted row
var colourNonHighlight = "#ffffff"; // background colour of non-
highlighted row
var tableID = "table1"; // ID of table

window.onload = function () {
// highlight the first row in the table
highlightRow();

// create event handler to process keypress events
document.onkeyup = function (e) {
if (window.event) {
e = window.event;
}
switch(e.keyCode) {
case 40: // down arrow pressed
currentRowIndex++;
break;
case 38: // up arrow pressed
currentRowIndex--;
break;
}
highlightRow();
}
}

// highlight the row specified by currentRowIndex
function highlightRow() {
var tbl = document.getElementById(tableID);

// check that currentRowIndex isn't outside the bounds of the table
if (currentRowIndex>(tbl.rows.length-1)) currentRowIndex =
tbl.rows.length-1;
if (currentRowIndex<0) currentRowIndex = 0;

// set all rows to non-highlighted
for (var i = 0;i<tbl.rows.length;i++)
tbl.rows.style.backgroundColor = colourNonHighlight;

// highlight current row
tbl.rows[currentRowIndex].style.backgroundColor = colourHighlight;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top