cancel
Showing results for 
Search instead for 
Did you mean: 

Can anyone help me ?

Can anyone help me ?

I am trying to use JQuery in my block functions, but I get an error saying JQuery not found. I tried google and some suggested to add it to local.xml but I think Magento2 does not have local.xml.

 

Can anyone help me on how to enable JQuery in Magento2?

 

Thanks

2 REPLIES 2

Re: Can anyone help me ?

You can set your jquery inside Block PHP file,

 $customJs = "
	require(['jquery'], function($){
		$(document).ready(function(){
			// code your javascript here
		});
	});
";
If Issue Solved, Click Kudos/Accept As solutions. Get Magento insight from
Magento 2 Blogs/Tutorial

Re: Can anyone help me ?

Hi @hgff_fghdfg

 

By default jQuery is always there and added in magento platform.

 

However if you would like to use the jQuery in the Block file, then you must require to load jQuery in the block file, using  below code you will able to load and put the js code on JS event :

 

$YourJsName = "
	require(['jquery'], function($){
	});
";

Note : Just one suggestion , jQuery always needs to use in view(phtml) file, if you don't have strict complex logic , then try to load and use it in the phtml file only.

 

Hope it helps 

if issue solved,Click Kudos & Accept as Solution