0

I am attempting to call a javascript function on a webpage that contains the 'this' keyword which is referring to the <input> textbox on the webpage. The function looks like this:

functiondostuff('hdnAttribute',this,'Key')

Using

js = "functiondostuff('hdnAttribute',this,'Key')"
Call IE.Document.parentWindow.execScript(js)

doesn't throw an error but does not produce the results of the function since this cannot be identified.

Stepping through the website this = [object DispHTMLInputElement] instead of the element name while the function is running. Anyone have any ideas?

Good Morning,

Adding more to this issue. There seems to be two problems, 1st is setting the window.event, functiondostuff begins with: if (window.event && window.event.keyCode == 13), when the function is called it exits out immediately due to the event being null. Is there a way to pass the event as 13 to the website? The second issue is submitting the "this" HTMLInputObject.

Does anyone know a method to fire the 'onkeypress' event? I am at the point of trying sendkeys to avoid calling the function but have not been able to get them to work with IE. Thanks for any suggestions!

Jay777
  • 13
  • 5
  • Why would it be the name? – epascarello Apr 01 '15 at 14:39
  • When the name of the object is tried it puts it in single quotes and then exits out of the function. – Jay777 Apr 01 '15 at 14:42
  • I am not sure what your problem is. If `this` is poinitng to the correct Html element, what is the problem? If you need the name it would just be referencing it. `function functiondostuff (att, htmlObj, prop) { console.log(htmlObj.name); }` – epascarello Apr 01 '15 at 14:46

1 Answers1

0

Key point is context. If you have this HTML

<input onclick="functiondostuff('hdnAttribute',this,'Key')">

then the browser can infer context from the user interaction and set this for you correctly.

From within VBA that's a slightly different matter and you have to define context manually.

How about this:

Dim js As Variant

js = Array( _
    "var input = document.getElementById('yourElementsId');", _
    "functiondostuff('hdnAttribute',input,'Key');" _
)

Call IE.Document.parentWindow.execScript(Join(js, vbNewLine))

This way you get to define context yourself.

document.getElementById was just for the sake of the example. If your element has no ID, use any other method (like DOM traversal, document.querySelectorAll, document.getElementsByTagName + a loop, ...) to get a reference to the desired element.

Tomalak
  • 332,285
  • 67
  • 532
  • 628
  • Tomalak, thank you for the suggestion! Ill give this a try today and let you know if it worked. – Jay777 Apr 01 '15 at 15:06
  • Still having issues getting this to work, trying the above sets 'this' as function(attr) { this.init(attr); this.tag=tag } when using a loop to get the elementbytaname. – Jay777 Apr 01 '15 at 18:35
  • I can't say what the error is without seeing the full code and knowing your full intent. But... Open that website in a browser. Open the browser's console. Write JS code that calls `functiondostuff()` with the right arguments. *That's* the exact code that you need to run through `execScript()` in VBA. – Tomalak Apr 01 '15 at 23:48
  • Hi Tomalak, thank you for the additional info. Stepping through the browser javascript it shows this as `[object DispHTMLInputElement]` when you press enter and `[function(attr) { this.init(attr); this.tag=tag }] ` when I use the array. – Jay777 Apr 02 '15 at 16:57
  • I have no idea what you mean, and without seeing the full code I never will. However, I cannot tell you any more than I have told you, really. – Tomalak Apr 02 '15 at 17:11