On Tue, 14 Oct 2014 16:23:39 +0200, Une Bévue wrote:
> Iv' tried eventDispatch but doesn't understand how it works really.
>
> Say, in my html i have a paragraphe :
> <p id='elem'>Listening to 'build' event...</p>
>
> it listen to the CustomEvent 'build' :
> elem.addEventListener('build', function (e) {
> console.log("'elem' listener: event.type = " + event.type);
> console.log("'elem' listener: event.detail = " + event.detail);
> },
> false
> );
>
> next, i have a button 'test' :
> <button id='test' data-date='14 oct 2014'>sendTest</button>
> which listen to two events :
> test.addEventListener('click', sendTest, false);
> test.addEventListener('build', receiveBuildTest, true);
>
> when the click event occurs the function sendTest create a new event and
> dispatch it :
> function sendTest(e) {
> console.log("sendTest(e) e:");
> console.dir(e);
> event = new CustomEvent('build', { 'detail': e.target.dataset.date });
> e.target.dispatchEvent(event);
> }
>
> well this new event is received by the 'test' element in the function
> receiveBuildTest :
> function receiveBuildTest(e) {
> console.log("receiveBuildTest(e) e:");
> console.dir(e);
> }
>
> BUT the paragraphe element 'elem' does not receive this event even if it
> listen to by :
> elem.addEventListener('build', function (e) {
> console.log("'elem' listener: event.type = " + event.type);
> console.log("'elem' listener: event.detail = " + event.detail);
> },
> false
> );
>
> Why ?
>
> In fact i don't understand why this event could only be recived on the
> element who has generated it, not interesting...
>
> as far as i understand...
>
> What i want to do :
>
> send an event to other parts of the dom from one having receive one from
> user click, by example.
Because dispatchEvent() trigger the event only on the element and its child
elements. So you need to call dispatchEvent() of the "elem" element object.
i.e.:
e = document.querySelector("#elem");
e.dispatchEvent(event);