20

I am adding a button dynamically to HTML as shown below.

When clicking that button I want to call a JavaScript function.

var but = document.createElement("button");

but.value="delete row";

but.setAttribute("onclick","callJavaScriptFunction()");
// this is not working

but.onclick="callJavaScriptFunction()"
// this is also not working

document.getElementById("but").onclick="callJavaScriptFunction()"
// this is also not working

but.id="but"+inc;

How can this be resolved?

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
sanjeev
  • 213
  • 1
  • 3
  • 6

10 Answers10

26

try this:

but.onclick = callJavascriptFunction;

or create the button by wrapping it with another element and use innerHTML:

var span = document.createElement('span');
span.innerHTML = '<button id="but' + inc +'" onclick="callJavascriptFunction()" />';
Micromega
  • 12,486
  • 7
  • 35
  • 72
jerjer
  • 8,694
  • 30
  • 36
  • dude everything is ok.. But I am not able to give the ID by concatinating the string : but and inc... Plz help...... "but' + inc +'" this is not working – sanjeev Aug 05 '11 at 15:01
  • try : span.firstChild.setAttribute("but" + inc); after the span.innerHTML, also i assume that inc is a variable, and that must be set before it is used. let me know it still doesn't work for you. – jerjer Aug 08 '11 at 04:00
  • 4
    how to add params to callJavascriptFunction() like callJavascriptFunction(p1,p2,p3) – Rahul Matte Jun 10 '16 at 08:20
  • It works, but how! What's the difference between `btn.onclick = function_name();` and `btn.onclick = function_name`. – Dman Cannon Jan 25 '21 at 06:41
15

Remove the () from your expressions that are not working will get the desired results you need.

but.setAttribute("onclick",callJavascriptFunction);
but.onclick= callJavascriptFunction;
document.getElementById("but").onclick=callJavascriptFunction;
Brett Walker
  • 3,566
  • 1
  • 18
  • 36
10

This code work good to me and look more simple. Necessary to call a function with specific parameter.

var btn = document.createElement("BUTTON");  //<button> element
var t = document.createTextNode("MyButton"); // Create a text node
btn.appendChild(t);   

btn.onclick = function(){myFunction(myparameter)};  
document.getElementById("myView").appendChild(btn);//to show on myView
user5276278
  • 101
  • 1
  • 2
6

Try
but.addEventListener('click', yourFunction) Note the absence of parantheses () after the function name. This is because you are assigning the function, not calling it.

Nivas
  • 18,126
  • 4
  • 62
  • 76
5

but.onclick = function() { yourjavascriptfunction();};

or

but.onclick = function() { functionwithparam(param);};

kennydelacruz
  • 114
  • 1
  • 4
  • > The significant drawback with inline events is that unlike event listeners described above, you may only have one inline event assigned. Inline events are stored as an attribute/property of the element[doc], meaning that it can be overwritten. (https://stackoverflow.com/a/6348597/1872046) – polkovnikov.ph Jun 26 '17 at 11:40
3

I was having a similar issue but none of these fixes worked. The problem was that my button was not yet on the page. The fix for this ended up being going from this:

//Bad code.
var btn = document.createElement('button');
btn.onClick = function() {  console.log("hey");  }

to this:

//Working Code.  I don't like it, but it works. 
var btn = document.createElement('button');
var wrapper = document.createElement('div');
wrapper.appendChild(btn);

document.body.appendChild(wrapper);
var buttons = wrapper.getElementsByTagName("BUTTON");
buttons[0].onclick = function(){  console.log("hey");  }

I have no clue at all why this works. Adding the button to the page and referring to it any other way did not work.

Seph Reed
  • 8,797
  • 11
  • 60
  • 125
  • 3
    I made this answer a very long time ago, and would no longer recommend it. Instead, use `btn.addEventListener('click', function(event){ do stuff });` – Seph Reed Jul 11 '17 at 23:28
2

Try this:

var inputTag = document.createElement("div");              
inputTag.innerHTML = "<input type = 'button' value = 'oooh' onClick = 'your_function_name()'>";    
document.body.appendChild(inputTag);

This creates a button inside a DIV which works perfectly!

Ciarán Bruen
  • 5,221
  • 13
  • 59
  • 69
hiTman
  • 21
  • 1
1
but.onclick = callJavascriptFunction;

no double quotes no parentheses.

nobody
  • 10,599
  • 4
  • 26
  • 43
0

Using modern JavaScript, this solution works well:

let btn = document.getElementById("btnID");
btn.onclick = () => {onAction(url, method);};
IPSDSILVA
  • 1,667
  • 9
  • 27
fullonic
  • 11
  • 1
0

for me this works!

button.onclick = () => (removechore());

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 25 '22 at 12:29