473,626 Members | 3,459 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to highlight a row when on click and keep it highlighted until next click

Hi

I'm not good at JS, but want to get more about it.

I want to use a JSP (the java code just used to get date, the rest are
html and javascript),

to display a table. the requirement is the all rows in even number in
light-blue, rows in odd number in light gray, when a mouse clicks on a
row, this row gets highlighted and in yellow, and it keeps highlighted
until next row is clicked on; plus when a row gets click, the data on
the row is saved to forward to (same as putting vales in input form
then forward to action form).

part of my code is

int color=0;

<tr onmousedown="th is.className='c urrent';"
onmouseup="this .className='cur rent'; saveRecord('<%= a%>',
'<%=b%>',
'<%=c%>');"
<%if (color%2==0){%>
onmouseout="thi s.className='ev en';" class="even"
<% } else { %>
onmouseout="thi s.className='od d ';" class="odd"
<% } %>

<td><%=e%></td>
<td>

...
function saverecord(...)

//<% %> is java code tag in jsp, used to get value

question
1.
result from above code, rows get highlighted when mouse moveing on it,
and can't keep highlighted for long (when mouse moves away,it is no
long in yellow).

2.
in saverecord function, what is the idea to put the valuables (a, b, c,
--they are global vars) in a hidden input form? or other way to
implement this?

--
Thanks
John
Toronto

Jul 23 '05 #1
1 5309
john woo wrote:
Hi

I'm not good at JS, but want to get more about it.

I want to use a JSP (the java code just used to get date, the rest are
html and javascript),

to display a table. the requirement is the all rows in even number in
light-blue, rows in odd number in light gray, when a mouse clicks on a
row, this row gets highlighted and in yellow, and it keeps highlighted
until next row is clicked on; plus when a row gets click, the data on
the row is saved to forward to (same as putting vales in input form
then forward to action form).

part of my code is

int color=0;

<tr onmousedown="th is.className='c urrent';"
onmouseup="this .className='cur rent'; saveRecord('<%= a%>',
'<%=b%>',
'<%=c%>');"
<%if (color%2==0){%>
onmouseout="thi s.className='ev en';" class="even"
<% } else { %>
onmouseout="thi s.className='od d ';" class="odd"
<% } %>

<td><%=e%></td>
<td>

...
function saverecord(...)

//<% %> is java code tag in jsp, used to get value

question
1.
result from above code, rows get highlighted when mouse moveing on it,
and can't keep highlighted for long (when mouse moves away,it is no
long in yellow).
Below is some code that highlights a row when they are clicked on.
The current highlight is removed when the next row is clicked on.

2.
in saverecord function, what is the idea to put the valuables (a, b, c,
--they are global vars) in a hidden input form? or other way to
implement this?


Always remember that if the user has JavaScript disabled or not
available, you will not get the global variables (they won't even
exist).

In your form, create a hidden field:

<form name="formA" onsubmit="retur n doForm(this);" ... >
<input type="hidden" name="globVars" ... >
...
</form>

In your script, create function doForm:

function doForm(f){
// Do form validation, if doesn't pass return false
// If does pass, put values into hidden field with your
// choice of delimiter:

f.globVars.valu e = a + ',' + b + ',' + c;

}

And the promised highlighting script:

<style type="text/css">
td {width: 5em;}
..even {color: #00FF00; background-color: #B3B3FF;}
..odd {color: #00FF00; background-color: #FFFF33;}
</style>

<script type="text/javascript">

// Variable to remember table, row and original
// colour of previous element.
var oldRow = { r:null , c:null };

// Highlight color (should use CSS)
var hc = '#CC0066';

function highlightRow(r) {

// If oldRow has been set, restore previous row
if ( oldRow.r ) {
oldRow.r.style. backgroundColor = oldRow.c;
}

// If a row was clicked on
if ( r && 'tr' == r.nodeName.toLo werCase() ){
// Store reference to row and its colour
oldRow.r = r;
oldRow.c = r.style.backgro undColor;
// Highlight clicked on row
r.style.backgro undColor = hc;

// Otherwise, reset oldRow values
} else {
oldRow.r = null;
oldRow.c = null;
}
}

function initTable(t){
if ( !document.getEl ementById || !document.body. style ) {return;}
var r = document.getEle mentById(t).row s;
var i = r.length;
while ( i-- ) {
r[i].onclick=functi on(){highlightR ow(this);};
}
}

window.onload = function() {initTable('tab leA')};

</script>
<table id="tableA">
<tr style="backgrou nd-color: #FFFF33;">
<td>blah</td><td>blah</td>
</tr>
<tr style="backgrou nd-color: #B3B3FF;">
<td>blah</td><td>blah</td>
</tr>
<tr class="odd">
<td>blah</td><td>blah</td>
</tr>
<tr class="even">
<td>blah</td><td>blah</td>
</tr>
</table>
<input type="button" value="Clear highlight" onclick="
highlightRow();
">

--
Rob
Jul 23 '05 #2

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

Similar topics

3
9280
by: Christophe Guillon | last post by:
Hello I would like to have a hypertext link which, when I click on it, leads to a web page where a certain word is highlighted. Depending on the link, the same target page would be displayed but with a different word highlighted. A bit like the search on google using the "cached" pages I have looked in several HTML tutorials, but I haven't found the answer...
2
302
by: Hari | last post by:
I want to highlight a row in a ASP.NET datagrid when i click on it ? How can i do that?
2
2075
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 =...
2
2437
by: Daniel Di Vita | last post by:
I have created an ASP.NET page that allows the user to page through a result set. I need to expand on this. On that same page I a filed where the user can type in a search string. When they click a button ALL the results will be returned and the closest match to the search string will be highlighted. The approach I am taking to page the data is to put the keys/indexes into an array then create another data reader based on those results to...
0
869
by: Franky | last post by:
I have two Mdi Child forms open. One has the title bar highlighted and the other does not. They each contain a control that is docked fill If I click the control in the one not highlighted it gets focus and I can use it, but the form's title bar does not get highlighted nor does the other form's title bar stop being highlighted.
0
1296
by: Franky | last post by:
I have a MDI form open and two child forms open in it. Each of the children contains a usercontrol which contains a richtextbox. The topmost form's caption bar is highlighted but I click on the other form's usercontrol, which is partially showing. (I don't click the caption bar, if I did it would become highlighted, but clicking the usercontrol does not highlight the caption bar.)
2
2225
by: yes_its_just_me | last post by:
Hi, I've seen this effect on other sites, but I don't know how to do it. What I'm trying to do is enable the user to highlight a portion of text on a webpage, then click an "edit" button and a little window will popup with that highlighted text in an editable mode (or it could be the entire paragraph tag that the highlighted text sits in). Then I want them to be able to edit the text however they want, click a "save" button, and the text...
3
3006
by: almurph | last post by:
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);
2
2685
GaryTexmo
by: GaryTexmo | last post by:
This is driving me nuts, I can't find anything after numerous web searches, I'm hoping someone here has some insight. What I'd like is for the selected text on a control (rich text box in this case) to remain highlighted when the control is inactive. To see an example of what I want, simply open notepad, type some things, select what you typed, then click another window. The text will remain highlighted even though notepad is inactive. To...
0
8202
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8707
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8641
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...
0
8510
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
7199
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...
1
6125
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5575
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4202
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1812
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.