[Solved] Is it possible to code in jQuery in a JS file? (.js)


Option -1:

Download the jquery file and store it in the directory of the project and add the script tag in your html file and then add second js file which you can write jquery

<script src="https://stackoverflow.com/questions/37921952/jquery.min.js"></script>
<script src="main.js"></script>  //Here you can write your custom js with jquery loaded first

Option -2 :

First take the script file you want to add jquery into and add the following lines of code

var script = document.createElement('script');
script.src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js";
script.type="text/javascript";
document.getElementsByTagName('head')[0].appendChild(script);

To test whether jquery actually loaded write this function in main.js file

$(document).ready(function() {
  console.log("Hello Jquery loaded in js file");
});

Suppose you are using main.js file then write the above 2 code snippets to implement and use the jquery in js file.

3

solved Is it possible to code in jQuery in a JS file? (.js)