Last active
August 1, 2020 18:58
-
-
Save sudikrt/1d3ea08a3777480979d93e7c1ada083b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div> | |
<ul id="category"> | |
<li id="laptop">Laptop</li> | |
<li id="cameras">Camera</li> | |
<li id="shoes">Shoes</li> | |
</ul> | |
</div> | |
<div id="form"> | |
<input type="text" id="name" data-uppercase/> | |
<input type="text" id="pan"/> | |
<input type="text" id="email" data-uppercase/> | |
</div> | |
<script src="./js/eventDelegation.js"></script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//single event listenet attached to parent | |
//its listening to all the child elements | |
//we have a issue of we directly redirect | |
// lets assume if we have child which is a or any other elemne | |
// if we do similar way then we are redirecting for each click | |
//Benefits | |
/** | |
* Memory | |
* Metigates the risk or performance bottelnex | |
* writing less code | |
* dom manipulation | |
*/ | |
//Limitation | |
/** | |
* All the events are not bubbled up | |
* Blur / focus / resizing / scrolling are not bubbled up | |
* if you are using stop propogation in code event delegation will not work | |
*/ | |
document.querySelector ('#category').addEventListener ('click', (e) => { | |
console.log ('Category parent clicked'); | |
console.log (e.target); | |
console.log (e.target.id); | |
console.log (e.target.tagName); | |
if (e.target.tagName === 'LI') { | |
console.log ('then only redirect or do some operation'); | |
//hence we are preventing unnecessary clicks | |
} | |
}) | |
document.querySelector('#form').addEventListener ('keyup', (e) => { | |
if (e.target.dataset.uppercase !== undefined) { | |
e.target.value = e.target.value.toUpperCase (); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment