custom event

I am trying to customize the default javascript event, namely click, code is in below : <!DOCTYPE html> <html> <body style="background-color: white;"> <div id="container"> <div id="parent"> <div id="child"></div> </div> </div> </body> <style> #container{ display: flex; justify-content: center; align-items: center; border: 5px solid black; margin: auto; width: 850px; height: 500px; } #parent{ display: flex; justify-content: center; align-items: center; border: 5px solid black; margin: auto; width: 450px; height: 300px; } #child{ border: 5px solid black; width: 300px; height: 150px; } </style> <script> const container = document.getElementById("container"); const parent = document.getElementById("parent"); const child = document.getElementById("child"); const customClick = new Event("click",{ bubbles : true, cancleable: false, compose: false }); container.addEventListener("click",function change(){ container.style.backgroundColor = "red"; }); container.removeEventListener("click",function change(){ container.style.backgroundColor = "red"; }); parent.addEventListener("click",function change(){ parent.style.backgroundColor = "yellow"; }); parent.removeEventListener("click",function change(){ parent.style.backgroundColor = "yellow"; }); child.addEventListener("click",function change(){ child.style.backgroundColor = "green"; }); child.removeEventListener("click",function change(){ child.style.backgroundColor = "green"; }); container.dispatchEvent(customClick); parent.dispatchEvent(customClick); child.dispatchEvent(customClick); </script> </html>
What I'm confused about this is how the callback function can be called before the event is being triggered
8 Replies
MarkBoots
MarkBoots8mo ago
I don't really understand your question. You are dispatching the customClick-event on the 3 elements in the last 3 lines. ah, i see what you mean. you removed the eventlisteners directly after creating them
Abrar
Abrar8mo ago
but sir can we customize the default event in javascript like click event because my goals is i want to make the click event doesn't bubble by using custom event and not used the stopPropagation( ) because if i can controll the bubble event with custom event i can make none bubble event be a bubble event.
Jochem
Jochem8mo ago
why not just use stopPropagation though?
Abrar
Abrar8mo ago
i want to controll the bubble event sir because i want to make none bubble event like animationend being bubble event
Zoë
Zoë8mo ago
container.addEventListener("click",function change(){
container.style.backgroundColor = "red";
});

container.removeEventListener("click",function change(){
container.style.backgroundColor = "red";
});
container.addEventListener("click",function change(){
container.style.backgroundColor = "red";
});

container.removeEventListener("click",function change(){
container.style.backgroundColor = "red";
});
These don't work because the function isn't the same. For this specific issue you want
const on_click = ({target}) => {
target.style.backgroundColor = "red" //Replaced `container` with just getting the target
}

container.addEventListener("click",on_click);
container.removeEventListener("click",on_click);

container.addEventListener("click",on_click, {once:true}); //Useful if you only want to run the event once, there are more options https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener
const on_click = ({target}) => {
target.style.backgroundColor = "red" //Replaced `container` with just getting the target
}

container.addEventListener("click",on_click);
container.removeEventListener("click",on_click);

container.addEventListener("click",on_click, {once:true}); //Useful if you only want to run the event once, there are more options https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener
Jochem
Jochem8mo ago
good catch! why though? It's best to be explicit when you're writing code, and if you overwrite the default handler and anyone else reads your code, they'll be confused no end. It can take hours just to figure out what the last guy was doing in your code base if you have stuff like that in there. Not that I think you can replace the click handler btw. You just definitely shouldn't.
Abrar
Abrar8mo ago
okay sir thanks for the explanation
Jochem
Jochem8mo ago
you can add e.stopPropagation() to a click handler snippet if you want, but 99% of the time you don't need to worry about propagation anyway