Create and manage custom events in javascript