N
A
V

Qty Selector

A quantity selector is a crucial UI element in e-commerce and POS systems, allowing users to easily increase or decrease the number of items they want to purchase or manage. A well-designed qty selector streamlines interaction and improves user satisfaction.

Key Features of a Qty Selector

  • Increment/Decrement Buttons: Plus and minus buttons for quick adjustment.
  • Manual Input: Allows users to type the desired quantity directly.
  • Validation Rules: Prevents entry of negative or invalid numbers.
  • Stock Awareness: Optionally limits selection based on available stock.
  • Responsive Layout: Adapts to all screen sizes and touch interactions.
  • Real-time Updates: Dynamically updates price totals or inventory counts.
  • Customizable Styling: Easily themed to match brand aesthetics.
  • Accessibility Friendly: Supports keyboard and screen reader interaction.

Qty Selector

1
Source Code

<div id="qtySelector" class="DyFx FxAnIsCr FxJyCtCr">

    <div class="qtyContainer DyFx FxJyCtSeBn Gp10 FxAnIsCr ZIx10 PnLt0 PnBm0 PgLtRt5">
        <span data-action="minus"><i class="CT-MinusCircleSolid CrBk CrurPr FtSe30"></i></span>
        <span data-action="qtyDisplay" class="CrBk CrurPr FtSe30">1</span>
        <span data-action="plus"><i class="CT-PlusCircleSolid CrBk CrurPr FtSe30"></i></span>
    </div>

</div>


document.addEventListener('DOMContentLoaded', function () {

    handleQtyChange('qtySelector');

});

function handleQtyChange(containerId) {

    let qtyContainers = document.getElementById(containerId).querySelectorAll('.qtyContainer');

    qtyContainers.forEach((container, index) => {
        let minusBtn = container.querySelector('[data-action="minus"]');
        let plusBtn = container.querySelector('[data-action="plus"]');
        let qtyDisplay = container.querySelector('[data-action="qtyDisplay"]');

        function generateRandomString(length = 10) {
            const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
            let result = '';
            for (let i = 0; i < length; i++) {
                result += chars.charAt(Math.floor(Math.random() * chars.length));
            }
            return result;
        }

        let qtyDisplayId = generateRandomString();
        qtyDisplay.setAttribute('id', qtyDisplayId);

        minusBtn.addEventListener('click', () => {
            let currentQty = parseInt(qtyDisplay.textContent);
            if (currentQty > 1) {
                qtyDisplay.textContent = currentQty - 1;
            }
        });

        plusBtn.addEventListener('click', () => {
            let currentQty = parseInt(qtyDisplay.textContent);
            qtyDisplay.textContent = currentQty + 1;
        });

        const InitValue = {
            id: qtyDisplayId,
            rows: 5,
            columns: 5,
            width: { mobile: "70vw", tablet: "150px", desktop: "200px" },
            zIndex: 'ZIx10000000000',
            // limit: 20, // maximum quantity limit
            callback: (qty) => {
                qtyDisplay.textContent = qty;
            }
        }

        qtyDisplay.addEventListener('click', () => {
            CT.CeQySr(InitValue)
        })

    });
}

Web Tool
Celebrate Tech © 2008 - 2024
Welcome Amit
  Logout
  Change Password
  Change Email

Loading...