Search code examples
javascripthtmlweberror-handlingreferenceerror

I get this error: Uncaught ReferenceError: $ is not defined at HTMLInputElement.onkeyup


As a part of a register form, I am just checking if name is typed or empty. Here's the HTML

<div class="text form-group justify-content-center mx-auto col-12">
     <input type="text" id="name" class=" " name="name" onkeyup='name_val();'>
    <label for="name"><span class="content">&nbsp;&nbsp;Name</span></label>
</div>
<p id="compare_name" class="text-center"> </p>

And here's the script.

var name_val = function (){
if($('#name').val() == ""){
    document.getElementById('compare_name').style.color = 'red';
    document.getElementById('compare_name').innerHTML = 'This field can\'t be empty';
}
else{
    document.getElementById('compare_name').innerHTML = '';
}}

I have referenced jQuery on top of all scripts. But i still get this error.

Uncaught ReferenceError: name_val is not defined at HTMLInputElement.onkeyup

I hope somebody could help me. Thanks in advance:)


Solution

  • Try this...

    function name_val() { ...

    // Content here...

    ... }

    I hope this will work...