469,336 Members | 5,552 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,336 developers. It's quick & easy.

Access rows of a created table


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

Mar 20 '06 #1
4 1366
I figured this out, and wanted to help anyone that might stumble on
this post

try document.getElementById('dgrList').rows

Mar 20 '06 #2
sc**********@gmail.com wrote:
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.

<URL:http://jibbering.com/faq/>
Or does anyone have a way to simulate the row clcik via javascript


<URL:http://developer.mozilla.org/en/docs/DOM:document.createEvent>
PointedEars
Mar 20 '06 #3
sc**********@gmail.com 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>

--
Rob
Mar 20 '06 #4
RobG said on 21/03/2006 9:16 AM AEST:
sc**********@gmail.com 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.


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

[...]

--
Rob
Mar 21 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Richard Dixson | last post: by
8 posts views Thread by Marcel Hug | last post: by
reply views Thread by comp974 | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Marylou17 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.