Why is my function not a function?

I have this on my browse page:

<input type="checkbox" id="toggle_SITEID" name="@@toggle_SITEID" onclick="toggle_SITEID(this)" /> New <br />

then in my js file i have this:

toggle_SITEID = function (chk) {
    // something
}

and then I check the box, "something" will not work. I check the firebug console error message:

toggle_SITEID is not a function

So why is that?

+3
source share
2 answers

There may be several reasons for this error:

  • You did not reference the script in the HTML file.

  • The function is not declared in the global scope, but in the inner scope.
    global area - WORKING DEMO
    domestic scale - NOT WORKING DEMO

  • , id (toggle_SITEID).
    Internet Explorer id document.getElementById(). , var.

  • , , , , , . , ...

    • , ,
    • , document

    , document, , .

    , : <a onclick="foo();">click me</a>, foo(), - document.foo = "bar". document , document.foo foo.

    . foo , . <a onclick="this.foo = 'bar'; foo();">click me</a>

+6

:

, .

:

toggle_SITEID = function (chk) {
    alert("I work!");
};

: http://jsfiddle.net/crazytonyi/cDNcu/

-2

All Articles