Mueyiwa Moses Ikomi Mueyiwa Moses Ikomi - 1 year ago 136
Javascript Question

get element id from array javascript

I have an array which is just id for buttons on mysite. I stead of getting the button id individually, ive put them in an array because I feel it'ld save me a lot of time.

var array = ['home', 'about', 'contact'];
for(i=0; i < array.length; i++){
var Button = document.getElementById(array[i]);

My question is how do I get individual button names I can use as reference.

Button.innerhtml just lists all contents of all 3 in d array instead of individually

Answer Source

Thanks for the help guys, but I was finally able to solve it using foreach loop.

 var array = ['home', 'about', 'contact'];

 array.forEach(function(item, index){

      var item = document.getElementById(item);

 Item.onclick = run function;

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download