How to write jquery in phtml file in magento 2 - Magesan

How to write jquery in phtml file in magento 2

How to write jquery in phtml file in magento 2

We need to write jquery sometimes in phtml file for special requirement. For that you need to define jquery in require function in script tag for using jquery within phtml file.
Put below script in phtml file and replace console.log with your custom code.

<script>
    require(['jquery', 'jquery/ui'], function($) {
        $(document).ready(function($) {
            console.log("custom jquery"); // write down your custom code from here.
        });
    });
</script>

Leave a Reply

Your email address will not be published. Required fields are marked *.

*
*
You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>