Link to home
Start Free TrialLog in
Avatar of huolto
huolto

asked on

Javascript: Get selected cell.cellIndex from a table in iframe

I have a iframe with a dynamically generated table. What I need to get is the cellindex value of the selected cell when a centain button is pressed. I need it so that i can define the row and the column where the selected cell is. The button is outside the iframe and i can't use onclick or any other event parameters in the iframe.

The selected cell which cellindex value i need has cursor in it, because it's in designmode. The cell in the table might have dublicate values, so i can't determine the cell by it's contents.

Is there an easy way to get the selected cells values?
Avatar of dfu23
dfu23

When you say that the cursor is in the cell I am guessing that you have input (text boxes?) in each of the cells?

If that is the case then you should be able to name the inputs (for example, txt_1_1) and be able to keep a page level variable that points to the input object wheneven one receives focus ... from there you can parse the id (or name) to find the row and column that it is in ...

HTH
You need to set the variables interactivly in the iframe.

In the head of the iframe page:

<script type="text/javascript">
<!--
therow='';
thecell='';
//-->
</script>

Then in the cells add this event:
onClick="therow=this.parentNode.rowIndex; thecell=this.cellIndex;">

Then in the event handler for the button on the main page you can reference the last value set:

localrow=top.frames[0].therow;
localcell=top.frames[0].thecell;

Cd&
Avatar of huolto

ASKER

Hi all.

Dfu, The frame is in design mode (just like in a wysiwyg editor), so that is why it can be edited.

The solution for adding id's to table doesn't work, because the idea was to have the table free of extra html.
Coboldinosaur, that is why I can't add onclick event or the variables inside the iframe.

Is it possible to get the rowIndex and cellindex from the iframe by clicking the button, with a command similar to your cell calling event.
Let's say the iframe is named Iframe.

<input type="button" value="get cellIndex" onclick="getCell(Iframe.this.cellIndex)">

function getCell(cellIndex){

var sel = Iframe.document.selection;
var theFrame = sel.createRange();
alert(theFrame.text);

}

gives out the text value of the cell, but i can't get the cellIndex value, which I need to check the row and the column.
ASKER CERTIFIED SOLUTION
Avatar of COBOLdinosaur
COBOLdinosaur
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The C grade without explanation is unacceptable.  I am asking for a review.

Cd&