So I have an array of buttons and when I try to assign for each one a click event like this
button[i].bind("click", function() ... it says the Object # has no method "bind".
What can be the problem ?
So I have an array of buttons and when I try to assign for each one a click event like this
button[i].bind("click", function() ... it says the Object # has no method "bind".
What can be the problem ?
Given the following HTML:
<button>Something</button>
<button>Something</button>
<button>Something</button>
You can obtain the array of buttons as follows:
var buttons = document.getElementsByTagName("button");
Then just pass the entire array to jQuery to bind the buttons:
$(buttons).click(function(){
alert("hello");
});
Or just select all the buttons to start with:
$("button").click(function(){
alert("hello");
});
The main point I'm trying to convey is that you most likely do not need to iterate over the button [] with .forEach.
if these buttons exist within a common ancestor it would be more performant to bind the click on that parent element.
$('.parent').on('click','button',function(e){
...
});
If these buttons have a common function then that makes life easier - if there is are different behaviours required for each function then look to use a data attribut on the button where you could specify a different method to execute for each.