locked
Make my button call the correct function

    Question

  • I can't seem to figure out where I made my probably obvious mistake. 

    HTML:

    <button id="yes" type="button">Yes</button>

    JS:

    // For an introduction to the Page Control template, see the following documentation:
    // http://go.microsoft.com/fwlink/?LinkId=232511
    (function () {
        "use strict";
    
        function yesFunction()/*tried with and w/o parameter*/ {
            console.log("In function");
        }
    
        WinJS.UI.Pages.define("/pages/yes.html", {
            ready: function (element, options) {
    
                var yesButton = document.getElementById("yes");
                yesButton.addEventListener("click", yesFunction, false);
            },
    
            yesFunction: function (buttonClicked) /*tried with and without parameter*/ {
                console.log("In function");
            }
            
        });
    })();
    Neither of the yes functions, the one inside the ready() or outside the ready(), are sending the message to the console

    Monday, April 22, 2013 8:16 PM

Answers

  • Change :

    yesButton.addEventListener("click", yesFunction, false);

    to

    yesButton.addEventListener("click", this.yesFunction.bind(this), false);

    Also remove the outer "yesFunction". Just keep the one within "ready" function.

    - Girija


    • Edited by Girija Beuria Monday, April 22, 2013 9:10 PM
    • Marked as answer by ZachAtttack Tuesday, April 23, 2013 2:53 PM
    Monday, April 22, 2013 9:05 PM