0

Here's the code I'm currently using

function firstChildAge() { 
    var header = document.createElement('H3');
    var body = document.getElementsByTagName('BODY');
    var textnode = document.createTextNode("WHAT IS THE AGE OF THE FIRST CHILD?");
    var inputChildOne = document.createElement("Input");
    var childOneAgeResponse = inputChildOne.value;
    header.appendChild(textnode);
    document.body.appendChild(header);
    document.body.appendChild(inputChildOne);
}

function submitButton() {
    var btn = document.createElement('Button');
    document.body.appendChild(btn);
    btn.onClick = testFunction_2();
}

function testFunction_2() {
    alert("foo");
}

if (childrenResponse == 1) {
    firstChildAge();
    submitButton(); 
}

As you can see, if childrenResponse (the user's response to a previous query) is equal to 1, both functions are activated. The attempted goal is to create a text node, an input, and a button. The button as of right now, should active testFunction2() which alerts us that it is working. But, testFunction2() activates before the text node or input even shows up. I can find the reason for this, and if anyone can help me out I'd greatly appreciate it. Thank you.

Also, on a side note, how can I add text to the button created in submitButton() ? Thanks!

Roman
  • 4,922
  • 3
  • 22
  • 31
M. Kaplan
  • 1
  • 2

4 Answers4

2

You have called the testFunction_2, instead of assigning it. This should work out fine.

function submitButton() {
    var btn = document.createElement('Button');
    btn.onclick = testFunction_2;
    document.body.appendChild(btn);
}
Jefree Sujit
  • 1,546
  • 5
  • 22
  • 38
1

You are calling the function testFunction_2() in onClick. You need to add event listener to button as shown below

btn.addEventListener('click', testFunction_2);

To add text to button use

var txt = document.createTextNode("CLICK ME");
btn.appendChild(txt);

Check the snippet below

function firstChildAge() {
  var header = document.createElement('H3');
  var body = document.getElementsByTagName('BODY');
  var textnode = document.createTextNode("WHAT IS THE AGE OF THE FIRST CHILD?");
  var inputChildOne = document.createElement("Input");
  var childOneAgeResponse = inputChildOne.value;
  header.appendChild(textnode);
  document.body.appendChild(header);
  document.body.appendChild(inputChildOne);
}

function submitButton() {
  var btn = document.createElement('Button');
  var txt = document.createTextNode("CLICK ME");
  btn.appendChild(txt);
  document.body.appendChild(btn);
  btn.addEventListener('click', testFunction_2);
}

function testFunction_2() {
  alert("foo");
}

childrenResponse = 1;
if (childrenResponse == 1) {
  firstChildAge();
  submitButton();
}
epascarello
  • 204,599
  • 20
  • 195
  • 236
Munawir
  • 3,346
  • 9
  • 33
  • 51
0

There were 2 problems:

  • onClick should've been onclick.
  • You were executing the function and assigning the result of that function to the onclick. btn.onClick = testFunction_2(); should be btn.onClick = testFunction_2;

See working snippet below.

function firstChildAge() { 
    var header = document.createElement('H3');
    var body = document.getElementsByTagName('BODY');
    var textnode = document.createTextNode("WHAT IS THE AGE OF THE FIRST CHILD?");
    var inputChildOne = document.createElement("Input");
    var childOneAgeResponse = inputChildOne.value;
    header.appendChild(textnode);
    document.body.appendChild(header);
    document.body.appendChild(inputChildOne);
}
function testFunction_2() {
    alert("foo");
}

function submitButton() {
    var btn = document.createElement('button');
    btn.innerHTML = "Some button name";
    btn.onclick = testFunction_2;
     document.body.appendChild(btn);
}

var childrenResponse = 1;

if (childrenResponse == 1) {
    firstChildAge();
    submitButton(); 
}

In javascript you can use the innerHTML set the button's HTML contents.

See Setting button text via javascript

btn.innerHTML = "This is a button name";

The Mozilla Developer Network is a good resource. Here's two links for the above mentioned snippets.

MDN innerHTML

MDN HTML Button element

jmathew
  • 1,522
  • 17
  • 29
0

You are calling the function testFunction_2 in onClick. You need to provide reference. That also won't work. You need to add event listener to button. And for setting the text, just set innerHTML of button.

var btn = document.createElement('Button');
btn.innerHTML = "click";
btn.addEventListener('click', testFunction_2);
document.body.appendChild(btn);

btn.onclick = testFunction_2; // in place of addEventListener.

// if you want to use onclick. use small case 'c' in onclick.

Ronn Wilder
  • 1,228
  • 9
  • 13