[SalesForce] Visualforce: Checkbox is not firing function onclick

I've tried everything I can think of to get this checkbox to work. This code is just supposed to trigger a function when a checkbox is checked. It currently does not trigger the alert, it only "visually" checks. Thank you for any input.

<apex:page >
    <script type="text/javascript">
    function checked() {
        alert("This has been triggered");
    }
    </script>
    <apex:form >
        <apex:inputcheckbox onclick="checked()"/>
    </apex:form>
</apex:page>

Best Answer

The problem is reported in the console:

Uncaught TypeError: checked is not a function

It seems to be a name conflict but I can't see where.

Changing the function name to e.g. isChecked works around the problem.

PS

The fix that relates to the root cause is this:

<apex:inputcheckbox onclick="window.checked()"/>

that references the global function checked that is otherwise shadowed (hidden) by the checked property of the input checkbox element.

Related Topic