Magento 2 – Call on Click Function in Custom Module Template File

magento2template

I want to call on click function in template file with script type "text/x-magento-init".

Then please tell me how can i do this?

Best Answer

Add below code in js file:

define(['jquery'], function ($) {
    'use strict';

    return function (config) { 
        console.log(config);
        $(document).on("click","input[id^='your-button-id-here']", function () {
             console.log('click');
        });   
    };
});

Add below code in template file:

<script type="text/x-magento-init">
{
   "*": {
     "Your component name here": {"url":"value"}
   }
}
</script>
Related Topic