473,769 Members | 5,885 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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.getEle mentById('table ID').style.back groundColor ="#000000";
document.getEle mentById('table ID').setAttribu te ("border", 2);
document.getEle mentById('table ID').setAttribu te(1, 1)

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

Thanking you,
Al.

Mar 14 '07 #1
3 3013
ASM
al*****@altavis ta.com 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.getEle mentById('table ID').style.back groundColor ="#000000";
document.getEle mentById('table ID').setAttribu te ("border", 2);
document.getEle mentById('table ID').setAttribu te(1, 1)
with down arrow

function hglg(e) {
var t = document.getEle mentById('table ID');
t.style.backgro und='#ffc';
t.style.color=' red';
t.style.border= '1px solid black';
if(e.keyCode == '40') {
t.style.backgro und='#000';
t.style.color=' white';
t.style.border= '2px solid yellow';
}
}

<body onkeypress="hgl g(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.getEle mentsByTagName( 'TD');
// to give onclick at each TD
for(var i=0; i<T.length; i++) T[i].onclick = function(){ hl(this);};
}

function hl(what) { // hightlighter function
// all TDs without class
for(var i=0; i<T.length; i++) T[i].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>

--
Stephane Moriaux et son (moins) vieux Mac déjà dépassé
Stephane Moriaux and his (less) old Mac already out of date
Mar 14 '07 #2
On 14 Mar, 10:17, "almu...@altavi sta.com" <almu...@altavi sta.com>
wrote:
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 colourNonHighli ght = "#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.onkeyu p = function (e) {
if (window.event) {
e = window.event;
}
switch(e.keyCod e) {
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.getEle mentById(tableI D);

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

// set all rows to non-highlighted
for (var i = 0;i<tbl.rows.le ngth;i++)
tbl.rows[i].style.backgrou ndColor = colourNonHighli ght;

// highlight current row
tbl.rows[currentRowIndex].style.backgrou ndColor = colourHighlight ;
}
Mar 14 '07 #3
ASM
al*****@altavis ta.com a écrit :
>
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.
with onclick :

http://stephane.moriaux.perso.orange...highlight_rows
http://stephane.moriaux.perso.orange...ighlight_cells
--
Stephane Moriaux et son (moins) vieux Mac déjà dépassé
Stephane Moriaux and his (less) old Mac already out of date
Mar 14 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2897
by: cpliu | last post by:
I have a page with 3 frames. The left one is the navigation, the top one is the graphic and title, the lower-right one is the content area. I'm wondering if it's possible to add a highlight to the menu item that its content is currently displayed in the right side. Since it's only 1 file for the navigation, it has to know what was clicked last and use a highlight graphics for that button after it was clicked or be able to detect its...
2
4945
by: J Krugman | last post by:
I have set up an HTML table with clickable cells (the cells contain only text). They work fine, but I would like to give the user some visual feedback to indicate that a cell has been clicked. I'd like this feedback to be the usual highlight on mouseDown, un-highlight on mouseUp, but I can't figure out how to do it. Help? Thanks in advance! jill --
15
2199
by: 50295 | last post by:
Hi everyone, This one is better experienced than explained, so I'm including a code sample below. Please and save (as an html file) and view with NN or Firefox (or maybe even Mozilla), and then view. When loaded: (1.) Place the mouse over "Top Menu" item. (The menu opens) (2.) Move to any of the sub-menu items. (3.) Click on the left or right (but NOT on the text) of the current
1
1990
by: Not Me | last post by:
Hey, In our database, we have implemented auditing simply by holding a copy of each row of the specific table at any time that data is updated. This gives us a long list of full records, often with only one change between them. Not sure if there's a better way to do this, but we'd like to somehow highlight whichever record was changed by comparing each row with the next (in date order).
8
3449
by: dhnriverside | last post by:
Hi I'm using an asp:repeater and its DataSource/DataBind system to show a number of records from the database. I want to highlight the latest record (which will be the highest ID). Any thoughts on how to do this with the repeater? Cheers
6
3441
by: LU | last post by:
A)I build a datagrid based on a calendar date selection. B)When user clicks a button column on the datagrid I want to highlight this row. I use the code below to highlight the row. ***** CODE TO HIGHLIGHT ROW Sub Datagrid_ItemCommand(ByVal s As Object, ByVal e As DataGridCommandEventArgs) If e.CommandName = "ViewUnitRequest" Then e.Item.BackColor = System.Drawing.Color.LightGray
9
3051
by: Devron Blatchford | last post by:
Hi there, Just wondering if I change the back and fore colour of a listview item when the mouse hovers over it? I want to overide the default windows colour. Can someone please tell me how to do this? Thanks Devron
2
2081
by: Sridhar | last post by:
Hi, I have a datagrid which contains 3 columns. Two of them are Bound Column types and the third one is Button Column. When I click on one of the rows in that Button Column, I would like to highlight that row. I have already set the SelectedItemStyle property in the designer But I am knowing when it will work. It is not highlighting the row. If I highlight the row inside the datagrid1_ItemCommand event using e.Item.BackColor =...
17
3385
by: toffee | last post by:
Hi all, I have a table with 12 cols and 10 rows. When a user clicks on a table cell; the page is refreshed and displays some data below the table dependant on whichever cell was selected. I would like to make it so that whichever cell was clicked; the background color is changed - so that when the user sees the data, (s)he can tell which cell it relates to. Does anyone know of a clever way to do this ?
0
10045
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9994
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9863
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8872
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5299
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3959
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3562
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.