<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by J on Medium]]></title>
        <description><![CDATA[Stories by J on Medium]]></description>
        <link>https://medium.com/@jitendra.gadhwal.iitd?source=rss-18e37a05f0bc------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Stories by J on Medium</title>
            <link>https://medium.com/@jitendra.gadhwal.iitd?source=rss-18e37a05f0bc------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 24 May 2026 21:27:50 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jitendra.gadhwal.iitd/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com" rel="hub"/>
        <item>
            <title><![CDATA[Custom events in JavaScript]]></title>
            <link>https://medium.com/@jitendra.gadhwal.iitd/custom-events-in-javascript-c4854a54bf30?source=rss-18e37a05f0bc------2</link>
            <guid isPermaLink="false">https://medium.com/p/c4854a54bf30</guid>
            <category><![CDATA[js-custom-events-use]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[html-events]]></category>
            <category><![CDATA[javascript-tips]]></category>
            <category><![CDATA[custom-events]]></category>
            <dc:creator><![CDATA[J]]></dc:creator>
            <pubDate>Mon, 05 Feb 2018 23:41:58 GMT</pubDate>
            <atom:updated>2018-02-06T01:47:11.458Z</atom:updated>
            <content:encoded><![CDATA[<p>I’m sure if you came to read this article you have already heard of or used some of core events defined on HTML elements like: onchange, onclick, onmouseover, onkeydown, onload etc.</p><p>These events are core events defined in HTML they give basic functionality like onchange fires when their is a change in input, textarea or select element of HTML but what will you do if you want to get notified (hide/show/update) some parts of your page when user type “why so serious!” in textarea field or let’s say you want to update multiple elements in your HTML page. Javascript custom events comes handy in these cases.</p><p><strong>But the best use case I personally see about events is that by using events we can create modular applications(mobile/web) so that at any time we can make changes to our apps without worrying too much about code breakage.</strong></p><p>Creating javascript custom events is simple, just use CustomEvent interface provided by javascript give event name (used later to listen so it should be unique for your app) and detail you want to pass to the listeners of this event.</p><blockquote><strong>var </strong><strong>myEvent = new </strong><strong>CustomEvent(&quot;customEventName&quot;, {</strong></blockquote><blockquote><strong>detail: { </strong><strong>data1: &quot;any value&quot;, name: &quot;any value you want to give&quot;, randomNumber: Math.round(Math.random() * 200)},</strong></blockquote><blockquote><strong>bubbles: true,<br>cancelable: true});</strong></blockquote><p>You can include any valid data types in detail object. If bubbles is true, events will bubble to ancestors of the element which fired the event or say ancestor of the element which fired the event can listen for this event default value of the bubbles is true. Events are only fired by HTML DOM elements simple syntax is: element.dispatchEvent(“yourEventName”)</p><p><a href="https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent"><strong>See all methods and properties of CustomEvent interface.</strong></a></p><blockquote><strong>document.body.dispatchEvent(myEvent);</strong></blockquote><p>In this case we are dispatching event on document.body element. You call dispatchEvent on the element that you want to act as your event target (aka the element that actually fires the event) like if you want to dispatch event on an element with id ‘msgbox’ you will write: document.getElementById(&quot;msgbox&quot;).dispatchEvent(myEvent);</p><p>Now it’s time to connect the other part of the story, event listeners. Just remeber few things about custom events:</p><p>(1) You have to put event listener code before the event dispatch code.</p><p>(2) You can’t attach event listener to the successor/child of the element which dispatch that event. See the plunker demo for better understadning.</p><p>(3) As Custom events extent Event object so all the Event properties are their to use, benefit of custom events is that we can specify our own properties or can override default events behaviour.</p><blockquote><strong>document.body.addEventListener(“</strong><strong>customEventName”, eventHandler, false);</strong></blockquote><blockquote><strong>function eventHandler</strong><strong>(e) { alert(&quot;Event detail is: &quot; </strong><strong>+ e.detail); }</strong></blockquote><p><a href="http://plnkr.co/edit/RWEKENpgtLGkGyf8r5h7?p=preview">Demo</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c4854a54bf30" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>