Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught ReferenceError: $ is not defined at http://localhost/project/public/user/1/edit:308:9 in Laravel [duplicate]

Jquery

<script src="http://localhost/project/public/plugins/jQuery/jquery-2.2.3.min.js"></script>

my code

<script type="text/javascript">
        function readURL(input){
          if(input.files && input.files[0]){
            var reader = new FileReader();
            reader.onload=function(e){
              $('#showimages').attr('src', e.target.result);
            }
            reader.readAsDataURL(input.files[0]);
          }
        }
        $("#inputimages").change(function(){
          readURL(this);
        });
        </script>


        <img src="" id="showimages">
        <input type="file" name="images" id="inputimages">

and i got this error

Uncaught ReferenceError: $ is not defined at http://localhost/project/public/user/1/edit:308:9

*line 308 -> $("#inputimages").change(function(){

Im new in js and jquery, Help me solve this error please...

I wrote the script out of the section. That was why the error occurred, see my answer.

like image 658
Fauzi PadLaw Avatar asked Nov 25 '16 09:11

Fauzi PadLaw


3 Answers

Thankyou for everyone!

This error is solved, in .blade.php laravel i have to write

@section('script')
<script type="text/javascript">
//my code here
</script>
@endsection
like image 127
Fauzi PadLaw Avatar answered Oct 18 '22 01:10

Fauzi PadLaw


To use Jquery, remember it has to import before using

Your problem is look like your Jquery is not imported. Maybe your path is incorrect or you imported it after your script.

First, check this url http://localhost/project/public/plugins/jQuery/jquery-2.2.3.min.js with with your web browser. If it exist you will see a lot of javascript code there.

Then to import it from your local server.

<script src="http://localhost/project/public/plugins/jQuery/jquery-2.2.3.min.js"></script>
<script>
      //your script here.
</script>

Another choice using cdn instead:

<script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
<script>
     //your script here
</script>
like image 33
Natsathorn Avatar answered Oct 18 '22 03:10

Natsathorn


Looks like jQuery wasn't loaded when your script is exectued.

You should load jQuery before scripts that are using it.

like image 1
Alexey Mezenin Avatar answered Oct 18 '22 03:10

Alexey Mezenin