Using a addEventListener to call a forEach function












1














So I cant find any sources online that shows an eventListener triggering a function by click that is forEach based I know i'm doing something wrong here so can any one point me to the right direction.



This is my code






document.addEventListener('DOMContentLoaded',function(){

var numbers = [4, 9, 16, 25];

document.querySelector('button').addEventListener('click',numbers.forEach(myFunction))

function myFunction(item, index) {

//create a new h1 element
var newH1 = document.createElement('h1');
//

//Insert content in the h1
var innerH1 = document.createTextNode(item);
//

//Add the text node to the newly created h1
newH1.appendChild(innerH1);
//

//Add the newly created element and its content into the DOM
document.body.insertBefore(newH1,document.body.secondChild);
//
}

});

<button>Click me</button>












share|improve this question
























  • Add }) to end of code
    – Mohammad
    Nov 22 at 9:43










  • What is your expected end result
    – Nedko Dimitrov
    Nov 22 at 9:43










  • secondChild isn’t a real thing. The rest looks OK
    – Ben West
    Nov 22 at 9:46










  • The problem is, that addEventListener expects a function reference/expression as the second argument, forEach returns undefined, hence no action is executed on a button click. Notice, that the iteration is executed immediately at the time you're attaching the listener.
    – Teemu
    Nov 22 at 9:50


















1














So I cant find any sources online that shows an eventListener triggering a function by click that is forEach based I know i'm doing something wrong here so can any one point me to the right direction.



This is my code






document.addEventListener('DOMContentLoaded',function(){

var numbers = [4, 9, 16, 25];

document.querySelector('button').addEventListener('click',numbers.forEach(myFunction))

function myFunction(item, index) {

//create a new h1 element
var newH1 = document.createElement('h1');
//

//Insert content in the h1
var innerH1 = document.createTextNode(item);
//

//Add the text node to the newly created h1
newH1.appendChild(innerH1);
//

//Add the newly created element and its content into the DOM
document.body.insertBefore(newH1,document.body.secondChild);
//
}

});

<button>Click me</button>












share|improve this question
























  • Add }) to end of code
    – Mohammad
    Nov 22 at 9:43










  • What is your expected end result
    – Nedko Dimitrov
    Nov 22 at 9:43










  • secondChild isn’t a real thing. The rest looks OK
    – Ben West
    Nov 22 at 9:46










  • The problem is, that addEventListener expects a function reference/expression as the second argument, forEach returns undefined, hence no action is executed on a button click. Notice, that the iteration is executed immediately at the time you're attaching the listener.
    – Teemu
    Nov 22 at 9:50
















1












1








1


0





So I cant find any sources online that shows an eventListener triggering a function by click that is forEach based I know i'm doing something wrong here so can any one point me to the right direction.



This is my code






document.addEventListener('DOMContentLoaded',function(){

var numbers = [4, 9, 16, 25];

document.querySelector('button').addEventListener('click',numbers.forEach(myFunction))

function myFunction(item, index) {

//create a new h1 element
var newH1 = document.createElement('h1');
//

//Insert content in the h1
var innerH1 = document.createTextNode(item);
//

//Add the text node to the newly created h1
newH1.appendChild(innerH1);
//

//Add the newly created element and its content into the DOM
document.body.insertBefore(newH1,document.body.secondChild);
//
}

});

<button>Click me</button>












share|improve this question















So I cant find any sources online that shows an eventListener triggering a function by click that is forEach based I know i'm doing something wrong here so can any one point me to the right direction.



This is my code






document.addEventListener('DOMContentLoaded',function(){

var numbers = [4, 9, 16, 25];

document.querySelector('button').addEventListener('click',numbers.forEach(myFunction))

function myFunction(item, index) {

//create a new h1 element
var newH1 = document.createElement('h1');
//

//Insert content in the h1
var innerH1 = document.createTextNode(item);
//

//Add the text node to the newly created h1
newH1.appendChild(innerH1);
//

//Add the newly created element and its content into the DOM
document.body.insertBefore(newH1,document.body.secondChild);
//
}

});

<button>Click me</button>








document.addEventListener('DOMContentLoaded',function(){

var numbers = [4, 9, 16, 25];

document.querySelector('button').addEventListener('click',numbers.forEach(myFunction))

function myFunction(item, index) {

//create a new h1 element
var newH1 = document.createElement('h1');
//

//Insert content in the h1
var innerH1 = document.createTextNode(item);
//

//Add the text node to the newly created h1
newH1.appendChild(innerH1);
//

//Add the newly created element and its content into the DOM
document.body.insertBefore(newH1,document.body.secondChild);
//
}

});

<button>Click me</button>





document.addEventListener('DOMContentLoaded',function(){

var numbers = [4, 9, 16, 25];

document.querySelector('button').addEventListener('click',numbers.forEach(myFunction))

function myFunction(item, index) {

//create a new h1 element
var newH1 = document.createElement('h1');
//

//Insert content in the h1
var innerH1 = document.createTextNode(item);
//

//Add the text node to the newly created h1
newH1.appendChild(innerH1);
//

//Add the newly created element and its content into the DOM
document.body.insertBefore(newH1,document.body.secondChild);
//
}

});

<button>Click me</button>






javascript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 at 9:44

























asked Nov 22 at 9:39









James Dean

604




604












  • Add }) to end of code
    – Mohammad
    Nov 22 at 9:43










  • What is your expected end result
    – Nedko Dimitrov
    Nov 22 at 9:43










  • secondChild isn’t a real thing. The rest looks OK
    – Ben West
    Nov 22 at 9:46










  • The problem is, that addEventListener expects a function reference/expression as the second argument, forEach returns undefined, hence no action is executed on a button click. Notice, that the iteration is executed immediately at the time you're attaching the listener.
    – Teemu
    Nov 22 at 9:50




















  • Add }) to end of code
    – Mohammad
    Nov 22 at 9:43










  • What is your expected end result
    – Nedko Dimitrov
    Nov 22 at 9:43










  • secondChild isn’t a real thing. The rest looks OK
    – Ben West
    Nov 22 at 9:46










  • The problem is, that addEventListener expects a function reference/expression as the second argument, forEach returns undefined, hence no action is executed on a button click. Notice, that the iteration is executed immediately at the time you're attaching the listener.
    – Teemu
    Nov 22 at 9:50


















Add }) to end of code
– Mohammad
Nov 22 at 9:43




Add }) to end of code
– Mohammad
Nov 22 at 9:43












What is your expected end result
– Nedko Dimitrov
Nov 22 at 9:43




What is your expected end result
– Nedko Dimitrov
Nov 22 at 9:43












secondChild isn’t a real thing. The rest looks OK
– Ben West
Nov 22 at 9:46




secondChild isn’t a real thing. The rest looks OK
– Ben West
Nov 22 at 9:46












The problem is, that addEventListener expects a function reference/expression as the second argument, forEach returns undefined, hence no action is executed on a button click. Notice, that the iteration is executed immediately at the time you're attaching the listener.
– Teemu
Nov 22 at 9:50






The problem is, that addEventListener expects a function reference/expression as the second argument, forEach returns undefined, hence no action is executed on a button click. Notice, that the iteration is executed immediately at the time you're attaching the listener.
– Teemu
Nov 22 at 9:50














1 Answer
1






active

oldest

votes


















2














You could try to put numbers.forEach(myFunction); to a function inside the event, like this:






document.addEventListener('DOMContentLoaded', function () {

var numbers = [4, 9, 16, 25];

document.querySelector('button').addEventListener('click', function () {
numbers.forEach(myFunction);
});

function myFunction(item, index) {

//create a new h1 element
var newH1 = document.createElement('h1');
//

//Insert content in the h1
var innerH1 = document.createTextNode(item);
//

//Add the text node to the newly created h1
newH1.appendChild(innerH1);
//

//Add the newly created element and its content into the DOM
document.body.insertBefore(newH1, document.body.secondChild);
//

}

});

<button>Click me</button>







Syntax:



selector.addEventListener(event, function () {
// implement...
});





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53427865%2fusing-a-addeventlistener-to-call-a-foreach-function%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    You could try to put numbers.forEach(myFunction); to a function inside the event, like this:






    document.addEventListener('DOMContentLoaded', function () {

    var numbers = [4, 9, 16, 25];

    document.querySelector('button').addEventListener('click', function () {
    numbers.forEach(myFunction);
    });

    function myFunction(item, index) {

    //create a new h1 element
    var newH1 = document.createElement('h1');
    //

    //Insert content in the h1
    var innerH1 = document.createTextNode(item);
    //

    //Add the text node to the newly created h1
    newH1.appendChild(innerH1);
    //

    //Add the newly created element and its content into the DOM
    document.body.insertBefore(newH1, document.body.secondChild);
    //

    }

    });

    <button>Click me</button>







    Syntax:



    selector.addEventListener(event, function () {
    // implement...
    });





    share|improve this answer


























      2














      You could try to put numbers.forEach(myFunction); to a function inside the event, like this:






      document.addEventListener('DOMContentLoaded', function () {

      var numbers = [4, 9, 16, 25];

      document.querySelector('button').addEventListener('click', function () {
      numbers.forEach(myFunction);
      });

      function myFunction(item, index) {

      //create a new h1 element
      var newH1 = document.createElement('h1');
      //

      //Insert content in the h1
      var innerH1 = document.createTextNode(item);
      //

      //Add the text node to the newly created h1
      newH1.appendChild(innerH1);
      //

      //Add the newly created element and its content into the DOM
      document.body.insertBefore(newH1, document.body.secondChild);
      //

      }

      });

      <button>Click me</button>







      Syntax:



      selector.addEventListener(event, function () {
      // implement...
      });





      share|improve this answer
























        2












        2








        2






        You could try to put numbers.forEach(myFunction); to a function inside the event, like this:






        document.addEventListener('DOMContentLoaded', function () {

        var numbers = [4, 9, 16, 25];

        document.querySelector('button').addEventListener('click', function () {
        numbers.forEach(myFunction);
        });

        function myFunction(item, index) {

        //create a new h1 element
        var newH1 = document.createElement('h1');
        //

        //Insert content in the h1
        var innerH1 = document.createTextNode(item);
        //

        //Add the text node to the newly created h1
        newH1.appendChild(innerH1);
        //

        //Add the newly created element and its content into the DOM
        document.body.insertBefore(newH1, document.body.secondChild);
        //

        }

        });

        <button>Click me</button>







        Syntax:



        selector.addEventListener(event, function () {
        // implement...
        });





        share|improve this answer












        You could try to put numbers.forEach(myFunction); to a function inside the event, like this:






        document.addEventListener('DOMContentLoaded', function () {

        var numbers = [4, 9, 16, 25];

        document.querySelector('button').addEventListener('click', function () {
        numbers.forEach(myFunction);
        });

        function myFunction(item, index) {

        //create a new h1 element
        var newH1 = document.createElement('h1');
        //

        //Insert content in the h1
        var innerH1 = document.createTextNode(item);
        //

        //Add the text node to the newly created h1
        newH1.appendChild(innerH1);
        //

        //Add the newly created element and its content into the DOM
        document.body.insertBefore(newH1, document.body.secondChild);
        //

        }

        });

        <button>Click me</button>







        Syntax:



        selector.addEventListener(event, function () {
        // implement...
        });





        document.addEventListener('DOMContentLoaded', function () {

        var numbers = [4, 9, 16, 25];

        document.querySelector('button').addEventListener('click', function () {
        numbers.forEach(myFunction);
        });

        function myFunction(item, index) {

        //create a new h1 element
        var newH1 = document.createElement('h1');
        //

        //Insert content in the h1
        var innerH1 = document.createTextNode(item);
        //

        //Add the text node to the newly created h1
        newH1.appendChild(innerH1);
        //

        //Add the newly created element and its content into the DOM
        document.body.insertBefore(newH1, document.body.secondChild);
        //

        }

        });

        <button>Click me</button>





        document.addEventListener('DOMContentLoaded', function () {

        var numbers = [4, 9, 16, 25];

        document.querySelector('button').addEventListener('click', function () {
        numbers.forEach(myFunction);
        });

        function myFunction(item, index) {

        //create a new h1 element
        var newH1 = document.createElement('h1');
        //

        //Insert content in the h1
        var innerH1 = document.createTextNode(item);
        //

        //Add the text node to the newly created h1
        newH1.appendChild(innerH1);
        //

        //Add the newly created element and its content into the DOM
        document.body.insertBefore(newH1, document.body.secondChild);
        //

        }

        });

        <button>Click me</button>






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 at 9:43









        Foo

        1




        1






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53427865%2fusing-a-addeventlistener-to-call-a-foreach-function%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Berounka

            Sphinx de Gizeh

            Different font size/position of beamer's navigation symbols template's content depending on regular/plain...