-5

I am answering a question on Stack Overflow and added some simple JavaScript code. I define a function, call the function once, and then setup an event listener on a button to also call the function. When the button is clicked, the following error is logged in the Result pane:

Error: { "message": "Uncaught ReferenceError: func is not defined", "filename": "https://stacksnippets.net/js", "lineno": 14, "colno": 61 }

The offending JavaScript code is:

const btn = document.getElementById('sub');

const appendNewTextArea = function() {
  const formEl = document.getElementById('form');
  const textareaTemplate = document.getElementById('textarea-template').innerHTML;
  const wrapper = document.createElement('div');
  wrapper.innerHTML = textareaTemplate;
  formEl.appendChild(wrapper);
}


// Call the function to create the first textarea
appendNewTextArea();

btn.addEventListener('click',appendNewTextArea);

Is this an error in the code? Or in how the code is run on Stack Overflow?

Question on Stack overflow - Display textbox multiple times

1 Answer 1

5

In your HTML markup you have this:

<button type="button" class="btn" id="sub" onclick="func()">Next</button>

That onclick calls func() and that function is nowhere to be found. The error is on your side, not an SE bug.

1
  • Argh, copy and paste got me again. Commented Aug 12, 2017 at 10:09

Not the answer you're looking for? Browse other questions tagged .