@@ -294,4 +294,118 @@ The function is only executed after the user stops typing for 300 milliseconds.
294294
295295References
296296
297- https://developer.mozilla.org/en-US/docs/Web/API/setTimeout
297+ - https://developer.mozilla.org/en-US/docs/Web/API/setTimeout .
298+
299+ ---
300+
301+ ### 9. Using the once Option in Event Listener
302+
303+ The once option lets an event listener execute only once, after which it automatically removes itself.
304+
305+ ``` html
306+
307+ <!DOCTYPE html>
308+ <html lang =" en" >
309+ <head >
310+ <meta charset =" UTF-8" >
311+ <meta name =" viewport" content =" width=device-width, initial-scale=1.0" >
312+ <title >Once Event Listener Example</title >
313+ </head >
314+ <body >
315+ <button id =" oneClickButton" >Click me only once</button >
316+
317+ <script >
318+ const button = document .getElementById (' oneClickButton' );
319+
320+ button .addEventListener (' click' , () => {
321+ alert (' Button clicked once!' );
322+ }, { once: true });
323+ </script >
324+ </body >
325+ </html >
326+ ```
327+ ** References**
328+
329+ - https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#parameters
330+ ---
331+
332+ ### 10. Handling focus and blur Events for Input Validation
333+
334+ The focus and blur events are helpful for form validation, showing messages when an input gains or loses focus.
335+
336+ ``` html
337+
338+ <!DOCTYPE html>
339+ <html lang =" en" >
340+ <head >
341+ <meta charset =" UTF-8" >
342+ <meta name =" viewport" content =" width=device-width, initial-scale=1.0" >
343+ <title >Focus and Blur Example</title >
344+ </head >
345+ <body >
346+ <input type =" text" id =" username" placeholder =" Enter username" />
347+ <p id =" message" style =" display : none ; color : red ;" >Username is required!</p >
348+
349+ <script >
350+ const input = document .getElementById (' username' );
351+ const message = document .getElementById (' message' );
352+
353+ input .addEventListener (' focus' , () => {
354+ message .style .display = ' none' ;
355+ });
356+
357+ input .addEventListener (' blur' , () => {
358+ if (! input .value ) {
359+ message .style .display = ' block' ;
360+ }
361+ });
362+ </script >
363+ </body >
364+ </html >
365+ ```
366+
367+ ** References**
368+ - https://developer.mozilla.org/en-US/docs/Web/API/Element/focus_event
369+ ---
370+ ### 11. Preventing Event Propagation
371+
372+ Sometimes, you might want to prevent an event from propagating to parent elements using stopPropagation.
373+
374+ ``` html
375+
376+ <!DOCTYPE html>
377+ <html lang =" en" >
378+ <head >
379+ <meta charset =" UTF-8" >
380+ <meta name =" viewport" content =" width=device-width, initial-scale=1.0" >
381+ <title >Stop Propagation Example</title >
382+ </head >
383+ <body >
384+ <div id =" outer" style =" padding : 20px ; background-color : lightcoral ;" >
385+ Outer Div
386+ <div id =" inner" style =" padding : 20px ; background-color : lightblue ;" >
387+ Inner Div
388+ </div >
389+ </div >
390+
391+ <script >
392+ const outer = document .getElementById (' outer' );
393+ const inner = document .getElementById (' inner' );
394+
395+ outer .addEventListener (' click' , () => {
396+ alert (' Outer div clicked' );
397+ });
398+
399+ inner .addEventListener (' click' , (event ) => {
400+ alert (' Inner div clicked' );
401+ event .stopPropagation (); // Prevents the outer div click from triggering
402+ });
403+ </script >
404+ </body >
405+ </html >
406+ ```
407+
408+ ** References**
409+
410+ - https://developer.mozilla.org/en-US/docs/Web/API/Event/stopPropagation
411+ ---
0 commit comments