Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Mar 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Help with using onClick='somefunction(this)'

    Greetings,

    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:
    Code:
    function exportStep1(button){
    		cwi_setSearchEnabled(false);
    		mdc_setColumnEnabled(false);
    		exportDiv.hide();		
    		cwi_buttonShowMessageUsingEnclosingForm(button,true);		
    	}
    And here's the where the button seems to be:
    Code:
    <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?

    THX

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,147
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    <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

  • #3
    New to the CF scene
    Join Date
    Mar 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:
    Code:
    HtmlDocument theDocument = theWebBrowser.Document();
    theDocument.InvokeScript("exportStep1('btnExportStep1Id_eventDiv')");

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,561
    Thanks
    80
    Thanked 4,495 Times in 4,459 Posts
    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:
    Code:
    <script>
    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
                );
    }
    </script>
    <body>
    <div id="framitz" style="background-color: pink; height: 40px; width: 100px;"
          onclick="show(this);">here</div>
    <div id="zamboni" style="background-color: green; height: 100px; width: 200px;"
          onclick="show(this);">there</div>
    </body>

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,561
    Thanks
    80
    Thanked 4,495 Times in 4,459 Posts
    Hard to tell, but I suspect you need to use
    Code:
    theDocument.InvokeScript("exportStep1(document.getElementById('btnExportStep1Id_eventDiv'))");
    Again, this is an object reference, and that's what getElementById returns.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,147
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Quote Originally Posted by Old Pedant View Post
    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. Technically this does not count as "mis-speaking" - rather it is "being economical with the truth".
    Last edited by Philip M; 03-18-2009 at 09:08 AM.

  • #7
    New to the CF scene
    Join Date
    Mar 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thanks to both of you

    I've got that bit working; on to the next obstacle!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •