I am new in magento2. I want to create an info link below every product. after clicking a link I want to show some info. I have created PHP file for anchor tag structure like \app\code\Vendor\Module\Block\Catalog\Product\info.php after clicking the link it calling js function from phtml file but I don't understand how to write js file inside js folder;

2 Answers 2




<?xml version="1.0"?>

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
        <script src="VendoreName_ModuleName::js/our_module_js_file_name.js" />



        $('#myAnc').click(function() {
            alert("hello i am click");


in your phtml file

<a id="myAnc" href="#">Click Here</a>

run magento command

php  bin/magento s:up 

php bin/magento s:s:d -f 

php bin/magento c:c

You can call JS and PHTML file from your custom module layout file:


<?xml version="1.0"?>

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd" layout="1column">
       <script src="Vendor_Module::js/pro_info.js"/>
        <referenceContainer name="content">
            <block class="Magento\Framework\View\Element\Template" name="custom_view" template="Vendor_Module::pro_info.phtml"></block>


<?php $proId = 12; // Here you can get dynamic Product Id. 
<a href="#" onclick="getProInfo(<?= $proId; ?>);">Get Product Info.</a>


function getProInfo(proId) {

I hope it will helpful for you.

  • thanks for answer but i have write anchor tag inside php file in block folder and js inside phtml file but it couldn't call js function giving error like: Uncaught ReferenceError: showInfo is not defined at HTMLAnchorElement.onclick
    – shital
    Commented Feb 17, 2020 at 11:35
  • Can you please post your code? Commented Feb 17, 2020 at 11:58
  • It's not proper way to use HTML code in php file, instead of block you can write anchor tag or HTML in PHTML file, you can call block function from PHTML file using $block->getProInfo() and get data in PHTML file and pass in JS function. Commented Feb 17, 2020 at 12:10
  • okk i will try this.
    – shital
    Commented Feb 18, 2020 at 13:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.