To fully utilize the jQuery script within your Next.Js application, follow these steps:
- Install jQuery using the command:
npm install jquery
- Create a loader.js file and add the following code:
window.$ = window.jQuery = require('jquery')
- Import the loader.js file:
import '[LoaderPath]/loader.js'
- Now, you can use jQuery within your components:
const handleForm = () => { $("#date").val("data"); }
Alternatively, you can place the jQuery script tag directly in your _document.js file, as shown in this example from pages_document.js:
import Document, { Html, Head } from 'next/document'
class MyDocument extends Document {
render() {
return (
<Html lang="id">
<Head>
<meta name="robots" content="noindex, nofollow"/>
<meta name="referrer" content="no-referrer-when-downgrade"/>
<script src="/assets/js/jquery.min.js"></script>
</Head>