James Dickens James Dickens - 1 year ago 117
Javascript Question

Including strings as parameters for a function chosen for .setAttribute to a button in javascript

I want to be able to pass along a string as a value for a button's onclick function using setAttribute. I am getting the error that "add is not defined" when I click on the button and I am not sure what I am doing wrong.

var AddButton = document.createElement("button");
var AddString = "add";
var SectorString = "1_1";

function AddOrDeleteDiv(AddString, SectorString) {

Answer Source

Do not use content attribute event handlers! Use event listeners instead:

var addButton = document.createElement("button");
var addString = "add";
var sectorString = "1_1";
addButton.addEventListener('click', function() {
  addOrDeleteDiv(addString, sectorString);
function addOrDeleteDiv(addString, sectorString) {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download