1

I have a javascript button (button A), when it is clicked, it generates some new elements, including another button (button B). I would like a way to listen on button B, and then execute a separate function.

I tried editing Button B's 'onclick' attribute in javascript. This did not work.

HTML:

 <input id="addTaskButton" type="submit" value="add task" onclick="addTaskFunction()"></input>

Javascript:

function buttonB()
{
// Not working
}

function addTaskFunction()
{
 var doneButton = document.createElement("BUTTON");
 doneButton.id = "doneButton";
 doneButton.innerHTML = "DONE";
 doneButton.onclick = "buttonB()";
}

i am expecting the listener to perform buttonB when ran. Instead, i get no response.

Alexandre Elshobokshy
  • 10,720
  • 6
  • 27
  • 57
John Doe
  • 108
  • 12

2 Answers2

2

Correct use is as follows

function addTaskFunction()
{
 var doneButton = document.createElement("BUTTON");
 doneButton.id = "doneButton";
 doneButton.innerHTML = "DONE";
 doneButton.onclick = buttonB;
}
doğukan
  • 23,073
  • 13
  • 57
  • 69
1

This works for me:

doneButton.onclick = function(){buttonB()};
Alex Morrison
  • 186
  • 5
  • 18