1

I'm trying to execute a JavaScript function. I get an error everytime the function enters the Ajax block saying:

$ is not defined

How can I solve this problem? I'm Programming a Google Chrome Addon and I get this error in my .js file:

enter image description here

At the moment ther is just the function above and its caller in the file:

// Calls get_pw function
document.addEventListener('DOMContentLoaded', function(){
    document.getElementById('submit').addEventListener('click', get_pw)
})

Many Thanks

Gucci
  • 921
  • 1
  • 7
  • 27
  • 1
    Include `jQuery`! – Hamza Abdaoui Mar 07 '18 at 08:04
  • 2
    `$` is a jQuery object. Perhaps if you want to continue this, include `jQuery` or proceed with the JS `XMLHttpRequest` – Tony Stonks Mar 07 '18 at 08:05
  • 1
    Include jQuery or make ajax call without jQuery as is told in this other question: https://stackoverflow.com/questions/8567114/how-to-make-an-ajax-call-without-jquery – Francisco Valle Mar 07 '18 at 08:06
  • OK thanks for the feedback I WOULD do this but I'm new to this topic and a big help would be if you could show me how for me its clear that i have to add something but not how - and i couldn't find an answer which worked – Gucci Mar 07 '18 at 08:08
  • why the downvote? - cant I ask one question on this page without getting hated? – Gucci Mar 07 '18 at 08:10
  • Possible duplicate of [Uncaught ReferenceError: $ is not defined?](https://stackoverflow.com/questions/2075337/uncaught-referenceerror-is-not-defined) – Hamza Abdaoui Mar 07 '18 at 08:13
  • @HamzaAbdaoui Abdaoui No, I'm working on my own project and encountered this problem – Gucci Mar 13 '18 at 08:41

2 Answers2

2

Include jQuery before executing the function or rewrite AJAX request without jQuery.

Including can be done simply by copying jQuery code at the beginning of your script or by loading a file from their CDN and then executing your script on a callback(slower).

Eugene
  • 905
  • 1
  • 10
  • 22
2

Download jQuery from https://code.jquery.com/jquery-3.3.1.min.js. Include it in head tag of your document and call get_pw() when document has loaded.

Faisal Rehman
  • 162
  • 1
  • 5