Preamble: I'm Italian, sorry for my bad English.
This is my problem:
I want to assign a function to a set of buttons.
I need to send a parameter to the function.
this is the code that I've tried:
function test(atxt) {
var buttons = $('.tblButton');
for (var i = 0; i < buttons.length; i++) {
buttons[i].onClick(sayHello(atxt));
}
}
function sayHello(txt){alert('hello' + txt)};
...getting the following error:
Uncaught TypeError: Object #<HTMLButtonElement> has no method 'onClick'
can you tell me where I went wrong and how can I fix it?
EDIT: I need iteration because I need the 'id of the button as a parameter of the function so i need to do buttons[i].onClick(sayHello(buttons[i].id))
Would this not work for your example: Do you have another reason for the iteration?
OR optionally if the elements are static and present:
Alternate approach: just change to this style:
see it working here: http://jsfiddle.net/dFBMm/
SO based on your note: this markup and code:
http://jsfiddle.net/dFBMm/1/
If you want to iterate through all of the buttons then you have to do that with
.each()
handler of the jquery:checkout the jsbin: http://jsbin.com/usideg/1/edit
Supposed to be
If you want to get the current button id then I think you are looking for this..