472,811 Members | 1,611 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,811 software developers and data experts.

How to return index value of td?

Below is a simplified version of table cell mouseover script, running
separate from the HTML code, that was posted on this group yesterday:

var d=document, td;

function mover(){
this.style.background="red";
td[0].style.background="lime"; // TEST: change 1st cell via any td mouseover
alert(this.td.style.index.value) // HOW TO RETURN THIS TD INDEX VALUE?
}

function mout(){
this.style.background="";
}

td=d.getElementById("myTable").getElementsByTagNam e("td");
for(var ii=0;ii<td.length; ii++){

td[ii].onmouseover=mover;
td[ii].onmouseout=mout;
}

-----------------------

I found I can easily affect any other separate td by simply:
td[0].style.background="lime";

However, I need to figure which cell the mouse is over in order to make
above "td[depend on this value]" and do a couple of if conditions to
produce slightly different result based on the offset of that value.

How could I retrieve the current index value? Along the lines of this
non-working syntax:
alert(this.td.style.index.value)

What would a correct method/syntax be? Is it the HTMLTableCellElement index
value and if so how can I return that value in the mover function?

Thanks,
michael

--

How much does it cost to entice a dope-smoking UNIX system guru to
Dayton?
-- Brian Boyle, UNIX/WORLD's First Annual Salary Survey
Jul 23 '05 #1
6 3192


michael wrote:

I need to figure which cell the mouse is over


A table cell has a property named
cellIndex
thus with
<td onmouseover="var cellIndex = this.cellIndex;"
you can get at the index.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #2
> <td onmouseover="var cellIndex = this.cellIndex;"

Thanks - I noticed however the cellIndex cannot be overwritten as I got:
"Error: setting a property that has only a getter"

And the "var" in mouseover handler didn't work as above, but worked fine
when omitted, as below:

<script>
function showIndex(){
document.forms.display.text.value = myCell;
}
</script>

<form name="display">
<input name="text" type="text">
</form>

<table cellpadding="10" cellspacing="0" border="1">
<tr>
<td onmouseover="myCell=this.cellIndex;showIndex()">ce ll 0</td>
<td onmouseover="myCell=this.cellIndex;showIndex()">ce ll 1</td>
<td onmouseover="myCell=this.cellIndex;showIndex()">ce ll 2</td>
<td onmouseover="myCell=this.cellIndex;showIndex()">ce ll 3</td>
</tr></table>

--

The problem ... is that we have run out of dinosaurs to form oil with.
Scientists working for the Department of Energy have tried to form oil
using other animals; they've piled thousands of tons of sand and Middle
Eastern countries on top of cows, raccoons, haddock, laboratory rats,
etc., but so far all they have managed to do is run up an enormous
bulldozer-rental bill and anger a lot of Middle Eastern persons. None
of the animals turned into oil, although most of the laboratory rats
developed cancer.
-- Dave Barry, "Postpetroleum Guzzler"

Jul 23 '05 #3
On Sat, 11 Sep 2004 18:58:33 +0200, michael <no****@example.com> wrote:
<td onmouseover="var cellIndex = this.cellIndex;"
Thanks - I noticed however the cellIndex cannot be overwritten as I got:
"Error: setting a property that has only a getter"


No, it can't. If you want to move a cell, you'll have to use methods like
replaceChild or insertBefore. For example:

// Get a reference to the table row
var row = this.parentNode;
// Move the current cell before the first cell
row.insertBefore(this, row.cells[0]);
And the "var" in mouseover handler didn't work as above,
It did work, but it kept the variable, cellIndex, local.
but worked fine when omitted, as below:

<script>
SCRIPT elements require a type attribute.

<script type="text/javascript">
function showIndex(){
document.forms.display.text.value = myCell;
}
</script>
[snip]
<td onmouseover="myCell=this.cellIndex;showIndex()">ce ll 0</td>


Why on earth create a global variable? Just pass the index to the function
as an argument:

function showIndex(i) {
document.forms['display'].elements['text'].value = i;
}

<td onmouseover="showIndex(this.cellIndex);">cell 0</td>

[snip]

Hope that helps,
Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #4
> Why on earth create a global variable? Just pass the index to the function
as an argument:


Thanks you - your information is being duly digested....

I was in fact trying to pass on TD value's in order to modify colors of
specific table cells etc., but now realise cellIndex is in fact starting at
zero at the beginning of each row.

I will use another example, and with table rows instead of TD's, which is
closer to what I'm trying to do.

The nice script below works by only giving a table id "myTable" to
highlight rows onmousover, without unecessary use of mouseover events in
each <td> tags, thus, separting the procedure from the html code.

But whilst the code being very generic, I still need some exceptions to the
rule, in that if for example, when the mouse hovers over the first row, to
display a different color in that row.

I guess I need to pass a "this.tr.length.value" or something like that onto
a variable of the mover function and then do an if statement within that
function. Something along the lines of:

<script type="text/javascript">
var d=document, tr;

function mover(){
if (EXCEPTION TO RULE HERE IF HOVERING OVER 1ST ROW){
this.style.background="purple";
}
else {
this.style.background="lime";
}
}

function mout(){
this.style.background="";
}

tr=d.getElementById("myTable").getElementsByTagNam e("tr");
for(var ii=0;ii<tr.length; ii++){

tr[ii].onmouseover=mover;
tr[ii].onmouseout=mout;

}

</script>

How could I pass that on from the loop below? I would greatly appreciate it
if someone could help me fill in the blanks here.

Many thanks!
michael

In the long run, every program becomes rococo, and then rubble.
-- Alan Perlis

Jul 23 '05 #5


michael wrote:
<td onmouseover="var cellIndex = this.cellIndex;"

I noticed however the cellIndex cannot be overwritten as I got:
"Error: setting a property that has only a getter"


I haven't claimed that you could set cellIndex and I don't think your
original question mentioned anything about changing the index. If you
want to change cells you need to use the DOM methods to remove/replace
child nodes as done in
http://www.faqts.com/knowledge_base/.../31307/fid/192
And the "var" in mouseover handler didn't work as above, but worked fine <td onmouseover="myCell=this.cellIndex;showIndex()">ce ll 0</td>


I strongly suggest to pass that value in as an argument to showIndex, no
need to have a side effect of creating a global variable:
function showIndex (index) {
...
}
<td onmouseover="showIndex(this.cellIndex);">

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #6
On Sat, 11 Sep 2004 22:38:51 +0200, michael <no****@example.com> wrote:

[snip]
But whilst the code being very generic, I still need some exceptions to
the rule, in that if for example, when the mouse hovers over the first
row, to display a different color in that row.

I guess I need to pass a "this.tr.length.value" or something like that
onto a variable of the mover function and then do an if statement within
that function. Something along the lines of:
Table rows have a property, rowIndex, that gives the index of the row
within the table (who would have thought :). You can use that to check if
the row in question is the first. But note: rowIndex returns the absolute
index. If you've structured your table with table sections (TBODY, THEAD
and TFOOT), you might want the sectionRowIndex property, which returns a
number relative to the beginning of the containing section.
<script type="text/javascript">
var d=document, tr;

function mover(){
if (EXCEPTION TO RULE HERE IF HOVERING OVER 1ST ROW){
if(0 == this.rowIndex) {
// first row
} else {
// other rows
}
this.style.background="purple";
Like DOM methods, you should check that you can access the style object
before attempting to do so.

if(this.style) {
this.style.background = 'purple';
}

[snip]
tr=d.getElementById("myTable").getElementsByTagNam e("tr");


You should definitely check that those methods are supported, and that you
get a return value from gEBI before performing that:

// Initialise with an empty array so that 'tr.length'
// can be evaluated without error if the rows are
// unattainable.
var tr = [];

if(d.getElementById) {
var t = d.getElementById('myTable');

if(t && t.getElementsByTagName) {
tr = t.getElementsByTagName('TR');
}
}
for(...

[snip]

Hope that helps,
Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #7

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

Similar topics

1
by: Pradeep Kumar | last post by:
I have a scenario where I am writing a property(using get and set) have to return a member of an object stored in an array of objects! The class user has to pass an index based on which a object i's ...
0
by: jeytu | last post by:
Hi all, I'm a newbie with .net. I have created a web method that would return a class. The definition of the class is <Serializable()> Public Class Response Dim l_status As Boolean ...
2
by: utab | last post by:
Dear all, I tried sth easy(actually this was an exercise) but I tried to use the standard lib. heavily for this problem(as far as I can). There was one point I could not figure out. The problem...
3
by: BombDrop | last post by:
Can any one help I have a method that will return a List to be bound as a datasource to a combobox see code for population below. I get the following error when i try to compile Error 29 ...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

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.