-2

i'm a newbie. i'm trying to call a function when i press the enter key. But it's not working. The error that i get on console is "Uncaught Reference Error: $ is not defined" on line 59. Can someone please help me to remove this. Thank you in advance. Below is the little piece of code of my program which is not working.

<body>
    <input id="sear" autocomplete="off" type="text" placeholder="Search here..." name="search" onkeyup="search(this.value)" />
    <button id="btnss" onclick="test()">Click</button>

    <script type="text/javascript">
    document.addEventListener("DOMContentLoaded", function() {
        $("#sear").keypress(function(e) { <!-- This is line 59 -->

            if (e.which == 13) {
                $("#btnss").click();
                return false;
            }

        });

    });
    </script>
</body>
Tushar
  • 85,780
  • 21
  • 159
  • 179
  • 2
    Include jQuery on page – Tushar Aug 21 '15 at 10:27
  • 2
    possible duplicate of [Uncaught ReferenceError: $ is not defined?](http://stackoverflow.com/questions/2075337/uncaught-referenceerror-is-not-defined) – CodingIntrigue Aug 21 '15 at 10:33
  • @RGraham The problem with the question you stated is the order of including libraries, not the same as this where jQuery is not included on page at all, so this is not duplicate questions of that – Tushar Aug 21 '15 at 10:36
  • @Tushar Doesn't matter. It's a duplicate of many, many, **many** other questions. Pick one. – CodingIntrigue Aug 21 '15 at 10:37
  • what do you think $ does? where have you got that code from? i would recommend to take one step back and learn the basics first... – DaniEll Aug 21 '15 at 10:38

2 Answers2

2

You haven't included jQuery on your page. Hence, you're getting the error $ is not defined.

Add following in your HTML page in <head> or at the end of <body> tags.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>

OR

If you don't want to use jQuery you can use Vanilla Javascript.

document.addEventListener("DOMContentLoaded", function () {
    document.getElementById('sear').addEventListener('keypress', function (e) {
        if (e.which == 13) {
            document.getElementById('btnss').click();
            return false;
        }
    }, false);
});
Tushar
  • 85,780
  • 21
  • 159
  • 179
  • Do people really use external sources for their JavaScript libraries? sounds like an error just waiting to happen to me – musefan Aug 21 '15 at 10:31
  • **Danger** That is an obsolete version of jQuery with known security errors. Always get the latest version of jQuery from the jQuery website. – Quentin Jan 31 '22 at 11:58
1

You should download the jquery.min.js file and link to that file in your HTML with this:

script src="js/jquery.min.js"

It will useful for offline development, while CDN online will work for online development

SuperBiasedMan
  • 9,814
  • 10
  • 45
  • 73
md umar
  • 11
  • 1