Magento 2 How to apply jquery for getting input text value in checkout page?

Magento 2 How to apply jquery for getting input text value in checkout page?

July 29, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento 2 How to apply jquery for getting input text value in checkout page?

Getting Input Text Value in Magento 2 Checkout Page Using jQuery

Hello developers! Today, we’re going to tackle a common question: How can you fetch input text values on the checkout page of your Magento 2 store using jQuery? Let’s dive right into it.

Understanding the Challenge

The main hurdle here is that the Magento 2 checkout uses KnockoutJS instead of jQuery for its core functionality. This means that while you may have success with jQuery elsewhere in your site, directly applying it to the checkout page can lead to some unexpected behavior or no response at all.

The Misleading jQuery Attempt

Let’s first examine the provided code snippet:

<script type="text/javascript">
    require([ 'jquery', 'jquery/ui'], function($){
        jQuery(window).load(function(){
            jQuery("#shipping-new-address-form input[name='vat_id']").keyup(function(e) {
                alert("test");
            });
        });
    });
</script>

At first glance, this seems like a solid approach. It initiates the script when the window loads, targets the desired input field using its name attribute, and triggers an alert every time a key is released within that field.

However, there are a few critical points to note:

  1. The require function ensures that jQuery and jQuery UI libraries are loaded before execution.
  2. The script attempts to bind the keyup event directly to the input field using jQuery syntax.

Why This Approach Fails

While this code works fine in other contexts, it falls short on the Magento 2 checkout page due to a few reasons:

  1. ** KnockoutJS Override**: Since the checkout is handled by KnockoutJS, any direct jQuery manipulation of DOM elements can cause conflicts or unexpected behavior.
  2. AJAX Communication: The checkout uses AJAX for updating the page content, which means that the traditional jQuery event bindings don’t always function as expected.

The Proper Approach: Extending Magento 2

To resolve this issue effectively, we need to extend Magento’s core functionality instead of trying to override it directly. This ensures that our changes integrate seamlessly with the existing knockout data-bindings and AJAX calls.

Here are the steps you should follow:

  1. Create a custom module (if you haven’t already) using Magento 2’s extension development guide.
  2. Within your module, create an XML layout file to define where you want to insert your script.
  3. In that file, use the script tag with the src_type="text" attribute to include your JavaScript code.

For example:

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="head.components">
            <script src_type="text" data-bind="attr: { 'data-mage-init': 'my_module/my_script'" }" />
        </referenceContainer>
    </body>
</page>

In this example, we’re using the head.components container to ensure our script is loaded before any other scripts on the page.

The Correct JavaScript Code

Now let’s revise the JavaScript code:

define([
    'jquery',
    'Magento_Checkout/js/model/quote',
    'Magento_Customer/js/customer-data'
], function ($, quote, customerData) {
    "use strict";

    return function () {
        var observer = new MutationObserver(function (mutationsList, observer) {
            mutationsList.forEach(function (mutation) {
                mutation.addedNodes.forEach(function (addedNode) {
                    if (addedNode.nodeName === 'FIELDSET') {
                        $(addedNode).find('input[name="vat_id"]').keyup(function (e) {
                            alert("test");
                        });
                    }
                });
            });
        });

        observer.observe(document, { childList: true, subtree: true });
    };
});

Here’s what we’ve done:

  1. We’re using a MutationObserver to monitor changes in the DOM tree.
  2. When a new FIELDSET element is added (which happens when Magento updates the page via AJAX), it triggers our jQuery code.
  3. The script targets the desired input field by its name attribute and binds the keyup event.

Conclusion

In summary, to fetch input text values on the Magento 2 checkout page using jQuery:

  1. Create a custom module with an XML layout file.
  2. Use a MutationObserver to monitor DOM changes for new elements.
  3. Target the desired field by its name attribute and bind your event listener.

By following these best practices, you’ll be able to extend Magento’s functionality without causing conflicts or unexpected behavior.

For more insights on Magento 2 development, make sure to explore our comprehensive guides at Laravel Company. We cover everything from theme customization to advanced backend modifications. Keep coding!

Magento 2 Checkout Database Themes

Our network