0

The below code works well on localhost using XAMPP. But it doesn't work on another server.

<!DOCTYPE html>
<html>
    <head>
        <script src="jquery-latest.js"></script>
    </head>
    <body>
        word: <input type="text" id="sub" />
        user: <input type="text" id="user" />

        <button type="button" id="btn">Click Me!</button>

        <script>
            $("#btn").click(function () {
                var word=$("#sub").val();
                var usr=$("#user").val();
                alert("hi");
            });
        </script>
    </body>
</html>

I have got 2 errors from Chrome inspect element:

  • Uncaught SyntaxError: Unexpected end of input jquery-latest.js:5669

  • Uncaught ReferenceError: $ is not defined

15
  • Do you have a copy of "jquery-latest.js" in the same folder of your html file? Commented Aug 12, 2012 at 5:14
  • Did you forget to deploy jquery-latest.js to the server? Commented Aug 12, 2012 at 5:15
  • 1
    sure..that file is there, I checked that first :) Some other problems i think Commented Aug 12, 2012 at 5:15
  • Try using the webkit inspector or Firefox's firebug tools to see if the jQuery javascript file is being loaded. One important thing to note is that Windows filesystems are case-insensitive, but most Linux file systems are case-sensitive, so check the jquery-latest.js file is the correct case. Commented Aug 12, 2012 at 5:17
  • 1
    @Strelok and davidbuzatto: - use of document ready is unnecessary when the code is in a script block at the end of the body: the script will be executed after any elements before it are already parsed. Commented Aug 12, 2012 at 5:21

2 Answers 2

1

check jquery-latest.js is same directory with html file. Otherwise code is ok and also works. add type in the script. try this

<script type="text/javascript" src="jquery-latest.js"></script>
Sign up to request clarification or add additional context in comments.

2 Comments

Uncaught SyntaxError: Unexpected end of input jquery-latest.js:5669 Uncaught ReferenceError: $ is not defined
Also you can include jQuery from some jQuery CDN: docs.jquery.com/Downloading_jQuery#CDN_Hosted_jQuery
1

You need to wire up the click handler in the $(document).ready() event.

<!DOCTYPE html>
<html>
    <head>
        <script src="jquery-latest.js"></script>
        <script>
        $(document).ready(function() {
            $("#btn").click(function () {
                var word=$("#sub").val();
                var usr=$("#user").val();
                alert("hi");
            });
        });
        </script>
    </head>
    <body>
        word: <input type="text" id="sub" />
        user: <input type="text" id="user" />

        <button type="button" id="btn">Click Me!</button>
    </body>
</html>

3 Comments

No you don't. The browser basically parses the document top to bottom, and script blocks can access elements above them since those elements have already been parsed and added to the DOM. In the OP's code the script block is at the end of the body, so all elements should be accessible from the code.
The scripts in head tag slows down the page rendering so it's better to place scripts at the end of body if possible.
@Jaison What was the solution?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.