0

This is the code I used to create the button:

var dfom = document.createElement('div');
dfom.setAttribute("id","options");
dfom.innerHTML = "<input type=\"button\" value=\"Say HI\" onclick=\get()\ />";
dfom.style.padding = '10px';
document.body.insertBefore(dfom, document.body.firstChild);

The button is created fine; it displays correctly. So to test it, I did this:

function get()
{
alert("HI");
}

However, when I click the button nothing happens. Any help, please? Thanks!

MathMan08
  • 59
  • 1
  • 10

4 Answers4

1

The function name should also be in quotes, i.e. onClick="get();". Also read up on Event Listeners whenever you get the time, for instace here.

Community
  • 1
  • 1
Milo Wielondek
  • 4,164
  • 3
  • 33
  • 45
1

If you want to write correct JavaScript code, then you should do it as follows:

var dfom = document.createElement("div"),
    button = document.createElement("input");

button.type = "button";
button.value = "Say HI";
button.addEventListener("click", function() {
    alert("HI");
}, false);

dfom.id = "options";
dfom.style.padding = "10px";
dfom.appendChild(button);

document.body.insertBefore(dfom, document.body.firstChild);

DEMO: http://jsfiddle.net/TKdYh/

VisioN
  • 143,310
  • 32
  • 282
  • 281
1

The \ should be \" around get(), but there are more considerations: namely the get function actually has to be defined outside of onload or another callback since it needs to be available globally. You would need a setup similar to this:

http://jsfiddle.net/CZ2y4/

...which defines get outside of the onLoad callback, which makes it available to onclick.

Also, use standard event bindings rather than onclick:

dfom.innerHTML = "<input type=\"button\" value=\"Say HI\">";
dfom.firstChild.addEventListener('click', get);

http://jsfiddle.net/CZ2y4/1/

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
  • ... and try to avoid `innerHTML`, since it is damned slow. – VisioN Mar 26 '13 at 23:01
  • 1
    +1 it was the backslashes without following double quotes that was the issue. The statement `the get function actually has to be defined before the element creation` is incorrect, a function definition will always be processed before any code is run, so it will always be defined before the button is created (if the button is created by code, as it is). – RobG Mar 26 '13 at 23:04
  • @VisioN—do you have anything to backup that assertion? – RobG Mar 26 '13 at 23:06
  • @RobG You're right; I think that if the function is defined in `onLoad` it would not be available globally hence this problem: http://jsfiddle.net/CZ2y4/2/ – Explosion Pills Mar 26 '13 at 23:08
0

The following works for me:

window.onload = function() {
  var dfom = document.createElement('div');
  dfom.id = "options";
  dfom.innerHTML = '<input type="button" value="Say HI" onclick="get()">';
  dfom.style.padding = '10px';
  document.body.insertBefore(dfom, document.body.children[0]);
}

function get() {
  alert("HI");
}

Some things to note:

  1. For standard DOM properties, it's usually better to just set the property, don't use setAttribute as it is inconsistently implemented.

  2. It's clearer to use single quotes for script and double quotes for makrup, that way you don't have to use as many backslashes to quote quotes

  3. Strictly, HTML attribute values only need to be quoted if they contain certain characters. It's easier to just quote them all rather than remember what that set of characters is. Browsers are also very tolerant so you can get away with unquoted characters that should be quoted in some browsers but not others.

RobG
  • 142,382
  • 31
  • 172
  • 209