[Solved] JavaScript function not called from onclick attribute correctly


Your code doesn’t work because you have a big scope problem. onclick attributes can only target functions that are in the global scope. In your case, your function is not in the global scope.

<script>
    $().ready(function(){
        function submitDB() {
            alert('From function!');
        }
    });
</script>

should be

<script>
        function submitDB() {
            alert('From function!');
        }
</script>

Demo: http://jsfiddle.net/ZeLXY/

I still don’t understand why “school” would try to teach how to add onclick attributes using jquery. There’s no reason you would EVER want to add onclick attributes to an element with javascript, much less jQuery.

0

solved JavaScript function not called from onclick attribute correctly