0

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>';
    };

};
1
  • 1
    The scope of the variable is in the addPage function, so you can't access it from addTextBlock, you would need to move it outside the function, somewhere in init Commented Jul 24, 2015 at 23:15

2 Answers 2

1

You have declared the variable wrapper inside the function addPage, so it is private to that function. Declare the variable inside init()

Try this

document.addEventListener("DOMContentLoaded", function() { init(); }, false);

function init() {
    var wrapper;
    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');
        wrapper = document.body.appendChild(newDiv);
    };

    function addTextBlock() {
        wrapper.innerHTML += '<p class="draggable">Text Here!</p>';
    };

};
Sign up to request clarification or add additional context in comments.

4 Comments

I think it's confusing to initialize wrapper to string (i.e., var wrapper; is probably better). But, yeah... this should work.
yeah, its should cast dynamically, though I have updated it to avoid confusion.
Thats it)) So simple... My brain stop working at all as I see. Thanks.
Yeah, it happens to me as well, take a break and start fresh :)
1

Call the function and add it as a parameter

 function addPage() {
            var newDiv = document.createElement('div');
            newDiv.setAttribute('class', 'wrapper');
            var wrapper = document.body.appendChild(newDiv);
            addTextBlock(wrapper);
        };

        function addTextBlock(a) {
            a.innerHTML += '<p class="draggable">Text Here!</p>';
        };

    };

Just to add some more: wrapper is in local scope and can only be used by the function it is in. So the DIV will append. As you move to your next function you leave the scope of the last function and wrapper is no longer available. You can declare var wrapper as a global but too many globals can get messy. In the method below function addTextBlock takes one parameter (a) You call the function from within the scope of the wrapper and pass wrapper as (a). This is then available for use in addTextBlock

1 Comment

That is not, what I need, cause "addPage" element created already with "addTextBlock". And I need to add it dynamically too... But thanks anyway...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.