I have a function that creates a couple of buttons. The problem is that the onclick attribute is not working. How can I use this attribute in JavaScript?
function querySuccess(tx, results) {
var len = results.rows.length;
for (var i=0; i<len; i++){
var name = results.rows.item(i).name;
var button = document.createElement("button");
button.className = "buttons";
button.value = name;
button.onclick = function() {
alert("assa");
};
var listIt = document.createElement("li");
listIt.className = "item";
listIt.appendChild(button);
var favs = document.getElementById("favs");
favs.appendChild(listIt);
}
}
submit(except in older IE due to bugs), so because you don'treturn falsefrom the event handler, if the button is in a form, the form will be submitted afterwards. Also settingvalueon the button does nothing (except in older IE due to bugs); if you want to add text to the button then append a text node to its content, or otherwise use an<input type="button">where value does correctly affect the text. But otherwise yeah... no obvious problem with calling the handler.