1

I hate to ask such a basic question, but I am having a hard time finding solid examples online. I'm green to all of this.

I have the following HTML Page:

<!DOCTYPE html>
<html lang="en">
<head>
 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" >
  $(function (Function1() {
   alert('Hello from JavaScript!');
  }));
 </script>
</head>
<body>
<div class="container">
    <button onclick="Function1()">
        Click me!
    </button>
</div>
</body>
</html>

I receive the following error in the chrome console on click:

Uncaught ReferenceError: Function1 is not defined

It has to be something obvious, but I don't know why my function call isn't working. I must have some kind of JQuery specific syntax wrong.

7
  • Well, it definitely isn't working; I'll give you that :) Commented May 23, 2013 at 0:53
  • 1
    I recommend reading docs.jquery.com/Tutorials:Getting_Started_with_jQuery Commented May 23, 2013 at 0:54
  • Could you recommend a good reference for function calls? Simple examples would be most helpful Commented May 23, 2013 at 0:55
  • Try reading "JavaScript: The Good Parts" (you can find it on Amazon or anywhere really). Commented May 23, 2013 at 0:57
  • 1
    I'll try to look them all up :) Thanks! Commented May 23, 2013 at 0:58

1 Answer 1

2

This is the fix for your issue: Your syntax was wrong.

<head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
     
    <script type="text/javascript">
        function Function1() {
            alert('Hello from JavaScript!');
        }
    </script>
</head>
<body>
    <div class="container">
        <button onclick="Function1()">Click me!</button>
    </div>
</body>

You could just create a click handler instead.

Example:-

Fiddle

$(function(){

    $('#clickMe').click(function(){
     alert('Hello from JavaScript!');
    })
})
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.