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