Javascript Events

…and triggering them

Recently I had an issue attempting to dispatch an event to a DOM element in a unit test. The particular element was a radio button and the way I would normally create an event:

var event  =  new Event('click',{'bubbles':true, 'cancelable':false})

object.dispatchEvent(event)

just didn’t work, this led to some digging about, looking through various documentation .. you know the score.

In the end I had to resort to a much older method of using object.click()

object.click() 
new MouseEvent('click', {bubbles:true});

more information:

https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/click

https://developer.mozilla.org/en-US/docs/Web/API/Event/Event

https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent