Access rows of a created table

S

scott.loomis

I have a javascript function that catches a click event of a row in a
table, but I am looking for a way to simulate a click in the first row
on page load.


function onRowClick(row)
{
//Do something



}


My table is actually a .Net Datagrid named dgrList, here is the grid


<asp:datagrid id="dgrList" runat="server" Width="448px"
BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px"
BackColor="White" CellPadding="3" GridLines="Horizontal"
autogeneratecolumns="False">


Now when I try to run something like
onRowClick(document.frmRemoteScan.dgrList.rows(0));


I get the message that dgrList is null or not an object, and when I
loop throught the elements in my form, dgrList is not one of them(yes
it is between the form tags).


My question, is there a way to reference the created table via
javascipt? Or does anyone have a way to simulate the row clcik via
javascript
 
S

scott.loomis

I figured this out, and wanted to help anyone that might stumble on
this post

try document.getElementById('dgrList').rows
 
T

Thomas 'PointedEars' Lahn

I have a javascript function that catches a click event of a row in a
table, but I am looking for a way to simulate a click in the first row
on page load.

function onRowClick(row)
{
//Do something
}

My table is actually a .Net Datagrid named dgrList, here is the grid


<asp:datagrid id="dgrList" runat="server" Width="448px"
BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px"
BackColor="White" CellPadding="3" GridLines="Horizontal"
autogeneratecolumns="False">

This is not what the client gets, so not what the client-side script has to
operate on.
Now when I try to run something like
onRowClick(document.frmRemoteScan.dgrList.rows(0));


I get the message that dgrList is null or not an object,

Then it is so.
and when I loop throught the elements in my form, dgrList is not one of
them(yes it is between the form tags).

Only form controls are part of the `elements' collection. A table is no
form control. And it should be `.rows[0]'.
My question, is there a way to reference the created table via javascipt?

There is. My first educated guess would be
document.getElementById("dgrList") -- with proper feature test --, but you
should really look at the generated source code, not the generating one.

Or does anyone have a way to simulate the row clcik via javascript

<URL:http://developer.mozilla.org/en/docs/DOM:document.createEvent>


PointedEars
 
R

RobG

(e-mail address removed) said on 21/03/2006 8:10 AM AEST:
I figured this out, and wanted to help anyone that might stumble on
this post

try document.getElementById('dgrList').rows

Note Thomas' reply, some (most?) browsers don't provide a click method
for all elements even though they support the onclick attribute.

<URL:http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-2651361>

If rowRef is a reference to a row, then rowRef.click() may 'work' in
some browsers (e.g. IE), but not others (e.g. Firefox). You need to use
createEvent to attach the event so you can click on it.


<script type="text/javascript">

function clickOn(id)
{
var el;
if ( document.getElementById
&& (el = document.getElementById(id))){

// Create an event object
if ( document.createEvent ){
var eventObj = document.createEvent("MouseEvent");

// Initialise properties
eventObj.initMouseEvent("click", true, true, window,
0, 0, 0, 0, 0, false, false, false, false, 0, null);

// Run the event
if (el.dispatchEvent) el.dispatchEvent(eventObj);

} else if (el.click) {
el.click();
}
}
}

function sayHi(){alert('hi');}

</script>

<input type="button" value="Attach click"
onclick="clickOn('tr01');">

<table border="1">
<tr id="tr01" onclick="sayHi();">
<td>Click the button to fire the onclick event</td>
</tr>
</table>
 
R

RobG

RobG said on 21/03/2006 9:16 AM AEST:
(e-mail address removed) said on 21/03/2006 8:10 AM AEST:



Note Thomas' reply, some (most?) browsers don't provide a click method
for all elements even though they support the onclick attribute.

<URL:http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-2651361>

If rowRef is a reference to a row, then rowRef.click() may 'work' in
some browsers (e.g. IE), but not others (e.g. Firefox). You need to use
createEvent to attach the event so you can click on it.

That should be: dispatch the event to run the onclick handler.

[...]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top