Vikum Dheemantha
Vikum Dheemantha

Reputation: 843

call a function onclick on some buttons without effecting the buttons individual onclick function (all ready assigned for button) - JavaScript

I am little bit new to JavaScript and still learning. I developed a dev with several buttons that have several function on onclick event. but I want to call another function when clicking any of these buttons. without affecting the normal functions of the button. (These common function change the parent div's height according to the individual function of the button).

I can simply put my common function inside each individual functions. But I want to know is there any other deffecrent way to call another function when click on all the buttons inside the particular div without manually putting the function to each buttons onclick event. Or at least each there a way to add event listener to all the child buttons of the div at once.

Only using pure JavaScript

Upvotes: 0

Views: 764

Answers (1)

Charlie Fish
Charlie Fish

Reputation: 20526

If you just add another click handler it should run multiple functions. Keep in mind you don't really have control over what order the event handlers are called. So be careful with that.

document.getElementsByClassName("buttonClass").addEventListener("click", myFunctionAction)

Keep in mind how you get the elements is up to you. The important part is the addEventListener. Doing that multiple times for an element will run more functions on click of the button.

Upvotes: 1

Related Questions