dimanche 15 novembre 2020

jQuery: How to reduce the number of event listeners?

I have a page with four different buttons and code that looks like this:

jQuery(".orange-button").on("click", () => {
   $(this).css("color", "orange");
   $(".display-img").addClass("#orange-color-image");
   ...
});

jQuery(".red-button").on("click", () => {
   $(this).css("color", "red");
   $(".display-img").addClass("#red-color-image");
   ...
});

jQuery(".blue-button").on("click", () => {
   $(this).css("color", "blue");
   $(".display-img").addClass("#blue-color-image");
   ...
});

...

Is there any way to condense down this code? They all do very similar things, yet with different classes and CSS applied.

Aucun commentaire:

Enregistrer un commentaire