Javascript stopPropagation

Created By: chatGPT

stopPropagation is a method that prevents the event from bubbling up the DOM tree. This is useful when you want to stop an event from triggering handlers on parent elements. Instead of the event propagating upward, it will only affect the current target element.
element.addEventListener('click', function(event) {
    // Prevent the event from propagating to parent elements
    event.stopPropagation();
    console.log('Button clicked');
});
You can use stopPropagation in various situations. Consider a scenario where you have a button inside a div, and both have click event listeners. If you want the button click to not trigger the div's click event handler, you use stopPropagation.
const button = document.querySelector('button');
const div = document.querySelector('div');

div.addEventListener('click', function() {
    console.log('Div clicked');
});

button.addEventListener('click', function(event) {
    event.stopPropagation();
    console.log('Button clicked');
});
This allows you to control the flow of events more precisely, ensuring that certain actions can occur independently of other listeners in the hierarchy. Always remember that while stopPropagation is powerful, it can also lead to unexpected behavior if overused, as it can break the event handling chain.
document.body.addEventListener('click', function() {
    console.log('Body clicked');
});
Introduction And SetupVariablesData TypesMethodsControl FlowFunctionsOperatorsData StructuresCommentsSyntaxEvent HandlingError HandlingPrimitive Data TypesReference Data TypesConstLetVarStringNumberBooleanNullUndefinedSymbolObjectArrayTemplate LiteralsArrow FunctionsRegular FunctionsCallApplyBindAsync FunctionsPromisesCallback FunctionsIf StatementElse StatementSwitch StatementFor LoopWhile LoopDo While LoopFor EachBreak StatementContinue StatementTry CatchThrowFinallyScopeClosureThis KeywordPrototypesInheritanceConstructor FunctionsClass SyntaxStatic MethodsGettersSettersSpread OperatorRest OperatorDestructuring AssignmentMap MethodFilter MethodReduce MethodEvent ListenersDom ManipulationInnerHTMLQuerySelectorAddEventListenerPreventDefaultStopPropagationTimeoutsIntervalsJsonLocal StorageSession StorageRegexMath ObjectDate ObjectString MethodsArray MethodsObject MethodsDeep CloningShallow CloningCallback HellPromise ChainingAsync AwaitMap Vs ForEachFilter Vs ReduceNullish Coalescing OperatorOptional ChainingTypeof OperatorInstanceof OperatorStrict EqualityLoose Equality