View Full Version : Help with using onClick='somefunction(this)'

03-17-2009, 10:14 PM

I am working on a windows service that will login to a company's website, then click a few buttons to start downloading my data. I've never worked with HTML or JS, so I'm a bit lost. I've been successful so far, until the end; The part I'm having trouble with is a button that calls a script with "this" as the parameter. I can't figure out which part of the DIV that "this" refers to!
Here's the function that's being called:

function exportStep1(button){

And here's the where the button seems to be:

<td><div id='btnExportStep1Id_mainDiv' style='position:relative'><div id='btnExportStep1Id_dataDiv' onMouseOver='activateButton(this)'><input id='btnExportStep1Id' name='btnExportStep1' type='HIDDEN'><table cellspacing='0' class='tbtn' border='0' cellpadding='0'><tr valign='middle'><td><img id='btnBgL' width='25' height='17' border='0' src='fw/themeBlue/image/buttons/btnTxtBgL1.gif'></td><td id='btnMiddle' align='middle' background='fw/themeBlue/image/buttons/btnTxtBgM1.gif' style='background-position: right;' nowrap><span id='btnLabel' style='color:#004882'>download</span></td><td><img id='btnBgR' width='8' height='17' border='0' src='fw/themeBlue/image/buttons/btnTxtBgR1.gif'></td></tr></table></div><div onClick='exportStep1(this)' id='btnExportStep1Id_eventDiv' onMouseOver='patchButtonMouseover(this)' onMouseOut='patchButtonMouseout(this)' style='position:absolute;top:0px;left:0px;width:0px;height:0px;overflow:hidden;cursor:pointer;cursor :hand;'></div></div>

The onclick is about 3/4 to the end of the line.
I can post the whole page if needed, but it's very long.

Can anyone help me?


Philip M
03-17-2009, 10:53 PM
<div id='btnExportStep1Id_dataDiv' onMouseOver='activateButton(this)'>
<div onClick='exportStep1(this)' id='btnExportStep1Id_eventDiv' onMouseOver='patchButtonMouseover(this)'

In each case the keyword this refers to the id of the <div>, and passes that id to the called function.

"A psychiatrist asks a lot of expensive questions that your wife asks for nothing." – Joey Adams

03-18-2009, 01:10 AM
Thanks, Philip for your quick response.
That's what I thought, but it doesn't work as expected....

Here's the calling bit from .NET:

HtmlDocument theDocument = theWebBrowser.Document();

Old Pedant
03-18-2009, 02:39 AM
Technically, Philip mis-spoke.

The "this" does NOT give you the ID of the DIV. It gives you a reference to the ENTIRE DIV.

Silly example:

function show( div )
alert("The id of the clicked on div is " + div.id
+ "\nIts background color is " + div.style.backgroundColor
+ "\nIts size is " + div.style.width + " by " + div.style.height
<div id="framitz" style="background-color: pink; height: 40px; width: 100px;"
<div id="zamboni" style="background-color: green; height: 100px; width: 200px;"

Old Pedant
03-18-2009, 02:41 AM
Hard to tell, but I suspect you need to use


Again, this is an object reference, and that's what getElementById returns.

Philip M
03-18-2009, 10:05 AM
Technically, Philip mis-spoke.

The "this" does NOT give you the ID of the DIV. It gives you a reference to the ENTIRE DIV.

Yes, indeed. I accept your very justified rebuke. :) I should have been more - er, well, pedantic. But in the context I understood that the OP "I've never worked with HTML or JS, so I'm a bit lost" wanted to pass the id. :p Technically this does not count as "mis-speaking" - rather it is "being economical with the truth".

03-19-2009, 09:03 PM
I've got that bit working; on to the next obstacle! :D