473,490 Members | 2,635 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 3232


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
2463
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
2461
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
2458
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
14984
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 ...
0
7112
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6974
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...
0
7146
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,...
0
7183
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...
0
5448
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,...
0
4573
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...
0
3074
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
628
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
277
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...

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.