Simple onclick error RRS feed

  • Question

  • Hey Guys.

    I use the JavaScript/HTML blank app template. I have this button and want it to call a function called btnClick(). I don't really think that anything is wrong with my HTML but here it is:

    EDIT: The problem is that I get an error message - "'btnClick is not definated"

    default.html :

        <h1 id="whatText">Some kind of secret text here...</h1>
        <div id="bodyPic"><button onclick="javascript:btnClick();" style="width: 300px; height: 300px;">Find out!</button></div>
        <div id="aswTxt"></div>

    default.js :

    (function () {
        "use strict";
        WinJS.Binding.optimizeBindingReferences = true;
        var app = WinJS.Application;
        var activation = Windows.ApplicationModel.Activation;
        app.onactivated = function (args) {
            if (args.detail.kind === activation.ActivationKind.launch) {
                if (args.detail.previousExecutionState !== activation.ApplicationExecutionState.terminated) {
                    // TODO: This application has been newly launched. Initialize
                    // your application here.
                } else {
                    // TODO: This application has been reactivated from suspension.
                    // Restore application state here.
        app.oncheckpoint = function (args) {
        function btnClick () {
            var rand = Math.round(Math.random());
            var asw;
            if (rand === 1) {
                asw = "David says yes!";
            else {
                asw = "David says no!";
            document.getElementById("aswTxt") = asw;

    I don't know if I wrote the function the wrong place or what? I didn't really know where to write it and that could be the problem. Also I tried to write onclick:"javascript:btnClick();" and without the JavaScript (in the HTML).

    • Edited by Potices Monday, April 22, 2013 6:31 PM Read EDIT.
    Monday, April 22, 2013 6:27 PM


  • The javascript: protocol isn't supported within apps (it's a security issue). You'll need to assign the handler from JS, which is good for separation of concerns anyway. That is:

    <button id="btnAnswer" style="width: 300px; height: 300px;">

    and in the onactivated handler:

    document.getElementById("btnAnswer").onclick = btnClick;
    And note that your last line of btnClick needs .innerText before the = asw part.


    • Marked as answer by Potices Tuesday, April 23, 2013 2:15 PM
    Monday, April 22, 2013 8:03 PM