Reputation: 1
When searching over the issues I found how to test browser events only with Jquery. But I'm not using it.
How can I trigger some event with Javascript (NOT Jquery)?
I have a simple html form and a button with 'onclick' event. I must simulate click event, so I can test it in Jasmine.
How can I do that?
P.S. I found some solution:
function fireClick(node){
if ( document.createEvent ) {
var evt = document.createEvent('MouseEvents');
evt.initEvent('click', true, false);
node.dispatchEvent(evt);
} else if( document.createEventObject ) {
node.fireEvent('onclick') ;
} else if (typeof node.onclick == 'function' ) {
node.onclick();
}
}
But it works only whit Jasmine standalone and not with Jasmine for node.js.
Upvotes: 0
Views: 253
Reputation: 1970
To simulate the "click" event:
var event = new Event("click");
element.dispatchEvent(event);
Upvotes: 1