I'm trying to generate synthetic Javascript events in an Internet Explorer extension, and I'm having trouble getting the fromElement property to stick. Here's an excerpt of my code:
MsHtml.IHTMLDocument4 doc4 = ... // the document object
Object o = null;
MsHtml.IHTMLEventObj2 eObj =
(MsHtml.IHTMLEventObj2)doc4.CreateEventObject(ref o);
// string that specifies the from element, e.g. "document.getElementById('id1')":
string locator = ...
object from = doc4.Script.GetType().InvokeMember("eval",
BindingFlags.InvokeMethod,
null,
doc4.Script,
new object[] { locator });
// from now holds a ref to an object that implements the IHTMLElement interface
eObj.fromElement = from;
IHTMLElement el = eObj.fromElement;
// el == null
What am I doing wrong here? eObj.fromElement should be equal to from, but it doesn't seem to be getting set.
Just a wild shot in the dark but could it be because your passing a "null" object to the CreateEventObject method? What about if you change this:
Object o = null;
To this:
Object o = new Object();
On line 3 of your example?
To find the right element you should try method getElementById from IHTMLDocument6: http://msdn.microsoft.com/en-us/library/cc288667
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With