jQuery is a lightweight, JavaScript library. The purpose of jQuery is to make it much easier to use JavaScript on your website. The jQuery library is available for you in default Magento 2 itself!
How to Use Magento jQuery Library?
To add the Magento jQuery library, you need to contain the jQuery code inside require function as below :
1 2 3 |
require(['jquery', 'jquery/ui'], function($) { //your js code goes here }); |
Here is an example :
1 2 3 4 5 |
require(['jquery', 'jquery/ui'], function($){ $(document).ready( function() { alert("Hello from Jigar Karangiya."); }); }); |
You can also use the above code in the browser console for direct execution and testing purposes and access elements using $ sign.
![Demonstrating use of jQuery Library in browser console](https://jigarkarangiya.com/wp-content/uploads/2023/07/image-10-1024x449.png)
You may also like :
How to use ifconfig in layout xml Magento 2?
How to Get Store Configuration value in Magento 2?
That’s it for this tutorial, See you in the next blog. Happy Coding !! 🙂