I have number of different buttons on my website and I want to assign different task to each button, currently I am referencing them one by one using getElementByID and because I have so many buttons I ended up writing endless lines of codes just to reference the buttons, having some knowledge of java I understand that what I have done could be achieved with less code using for loop but I am not quite sure how to go about doing that, any help is welcomed.
Currently this is how I am referencing my buttons:
var Button1Menu = document.getElementById("button1menu");
var Button2Menu = document.getElementById("button2menu");
var Button3Menu = document.getElementById("button3menu");
var Button4Menu = document.getElementById("button4menu");
var Button5Menu = document.getElementById("button5menu");
var Button6Menu = document.getElementById("button6menu");
var Button7Menu = document.getElementById("button7menu");
var Button8Menu = document.getElementById("button8menu");
var Button9Menu = document.getElementById("button9menu");
.....
.....
.....
var Button43Menu = document.getElementById("button43menu");
and then I am assigning click listener to each one:
Button1Menu.onclick = function() {
//doing something
};
Is there a better way of achieving this please.