I need to dynamically add element, that also been created dynamically. I made two buttons. First must add first element, another - add another element to previously created element. But I got an error, because variable, created in first function, cant reach second function (I got "wrapper is not defined").
What Im doing wrong?
document.addEventListener("DOMContentLoaded", function() { init(); }, false);
function init() {
textButton = document.getElementById('textButton');
pageButton = document.getElementById('pageButton');
pageButton.addEventListener('click', addPage, false);
textButton.addEventListener('click', addTextBlock, false);
function addPage() {
var newDiv = document.createElement('div');
newDiv.setAttribute('class', 'wrapper');
var wrapper = document.body.appendChild(newDiv);
};
function addTextBlock() {
wrapper.innerHTML += '<p class="draggable">Text Here!</p>';
};
};
addPagefunction, so you can't access it fromaddTextBlock, you would need to move it outside the function, somewhere ininit