As it says in the title, I'm trying to create a button element in JavaScript, but when I try to set its onclick attribute, it just ignores it. I've looked at other questions with this topic on this site (such as this one), but everyone answers to put it in with HTML, like so:
<button id="buttonid" onclick="click(this);">
But my program dynamically creates the HTML buttons entirely with JavaScript (they're aligned on a table, so using a for loop is very useful. Plus I'm more familiar with JavaScript). Like this:
var b = document.createElement("button");
b.id = "buttonid";
b.onClick = "click(this);"; //I've tried many variations with no success
document.appendChild(b);
The button element is never declared in the HTML. I need a way to add the onclick attribute using JavaScript alone. I'm not very experienced with JQuery, so I would prefer just JavaScript.
Thank you in advance.
PS, I've tried all the following methods with both lowercase .onclick and camelcase .onClick:
b.onClick = "click(this);"; //just simply doesn't work
b.onClick = click(this); //actually runs the program there, with some ambiguous "this" object
b.onClick = click; //passes the actual function, but disallows the ability to pass the button's object (via "this")