After much searching and playing with various solutions, I found one at this page:
http://stackoverflow.com/questions/590289/javascript-event-that-fires-without-user-interaction/590339#590339
This provides an easy method for programmatically invoking native events such as “onchange”.
Here it is:
// this supports trigger native events such as 'onchange'
// whereas prototype.js Event.fire only supports custom events
function triggerEvent(element, eventName) {
// safari, webkit, gecko
if (document.createEvent)
{
var evt = document.createEvent('HTMLEvents');
evt.initEvent(eventName, true, true);</code>
return element.dispatchEvent(evt);
}
// Internet Explorer
if (element.fireEvent) {
return element.fireEvent('on' + eventName);
}
}
This is primarily to deal with prototype.js not allowing the firing of native events (which jQuery does).
Here is another approach I found but have not tried which adds the capability to prototype.js: event.simulate.js
Advertisement
Filed under: Code
[...] http://benjchristensen.com/2010/08/28/trigger-native-javascript-events-with-prototype-js/ This entry was posted in Blogs and tagged events. Bookmark the permalink. [...]
And this is the world where Selenium lives and Rules
[...] I found was a nice post by Ben Christensen talking about this exact problem. The code on his page creates a function that will trigger events [...]
Thanks for your post. I’d just add how to call the function:
// in prototype:
triggerEvent($(‘someElementId’),’change’);
// in js:
triggerEvent(document.getElementById(‘someElementId’), ‘change’);