jquery onclick not running magento 2

jquery onclick not running magento 2

July 20, 2026 · Illustrated author avatarBy Stefan Bogdanescu
jquery onclick not running magento 2

Magento 2 jQuery onClick Not Working: Debugging Your Code

If you’re facing a situation where your jQuery click event isn’t firing in your Magento 2 module, don’t worry—you’re not alone! Let’s dive into the common issues and troubleshooting steps to get your script working.

Understanding Requirements.js

In Magento 2, all JavaScript libraries are loaded through the requirejs framework. When you want to use jQuery in a view file like .phtml, you need to use the require function to ensure that jQuery is available before your code runs.

Your current script looks correct:

<script>
    require(['jquery'],function($){
        $('input[name=answer]').on('click', function(){
            alert('You have selected a button');
        });
    });
</script>

Here, the require function loads the jQuery library and passes it to your anonymous function as the parameter $. Inside this function, you’re targeting all radio buttons with the name ‘answer’ and binding the click event.

Ensure Your Script is Placed Correctly

The script must be placed within a <script> tag inside your view file. In your case, it seems to be correctly embedded in the content of your .phtml file.

Check for JavaScript Errors

Open your browser’s developer tools (right-click > Inspect Element or press F12) and look at the Console tab. If there are any syntax errors or issues with the script loading, they should appear here. However, you mentioned that no errors were showing up in your case.

Debugging Steps to Follow

1. Check if requirejs is loaded correctly

Ensure that Magento has properly set up its requirejs configuration by checking the app/design/frontend/{vendor}/{theme}/Magento_Theme/layout/default.xml file:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc:layout_generated.xsd">
    <referenceContainer name="head.components">
        <block class="Magento\Theme\Block\Html\Head" name="requirejs" as="scripts" group="js">
            <action method="addJs" implementation="Magento\RequireJs\Model\RequiredJavaScript">
                <argument name="files" xsi:type="array">
                    <item name="jquery">lib/jquery/lib/jquery.js</item>
                    <!-- Other libraries -->
                </argument>
            </action>
        </block>
    </referenceContainer>
</config>

2. Check if jQuery is correctly loaded

Right-click on a blank area of your page and select “Inspect Element” or press F12 to open the developer tools. Go to the Console tab, type $.fn.jquery (or just jQuery), and hit Enter.

If jQuery is properly loaded, you should see its version number displayed.

3. Use a Data attribute for your radio button

Sometimes, Magento’s default styles or scripts interfere with jQuery bindings. To ensure that your script has priority over any other binding:

<form method="POST" action="../mymodule">
    <input type="radio" name="answer" value="A" required data-role="my-radio-button"/>
</form>

Then, in your JavaScript code:

require(['jquery'],function($){
    $('input[data-role=my-radio-button]').on('click', function(){
        alert('You have selected a button');
    });
});

Conclusion

If you’ve double-checked all the points above and still can’t get your script to work, there might be an issue with Magento’s JavaScript compilation process or cache settings. Ensure that you have cleared your var/cache folder and run bin/magento setup:upgrade and bin/magento setup:static-content:deploy commands.

Remember, when working with JavaScript in Magento 2, it’s crucial to understand the requirejs ecosystem.

For more detailed tutorials on working with jQuery and other scripts in Magento 2, check out our comprehensive guides at Laravel Company.

Magento 2 Database Extensions Themes

Our network