Jump to content

Limit Product Qty on Product

Go to solution Solved by paul2009,

Recommended Posts

Posted (edited)

 

I am trying to limit the qty a customer can order using the below script. The script is supposed to change the quantity input tag to have max="5", but it is not working.

Is the issue with my code, or something to do with SquareSpace? I do know that the page loads with max="9999" by default, is there something I'm missing to override that?

<script>
   var qty = document.querySelectorAll('input[type="number"]');
   qty.setAttribute('max', '5'); 
</script>

 

Edited by Jamz
  • Replies 2
  • Views 326
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

  • Solution
Posted
51 minutes ago, Jamz said:

I am trying to limit the qty a customer can order using the below script. The script is supposed to change the quantity input tag to have max="5", but it is not working.

You are using the method querySelectorAll(). This returns a NodeList that represents all the quantity selectors in the document/page. As it is a list (and not an individual element) you cannot set the max attribute for all the matching elements with one command. That would require jQuery 🙂

In JavaScript, if you want to modify all the elements on the list you'll need to iterate through them and set the attribute of each one. However, as it looks like there is only one qty selector on this page, you should be able to fix it by changing querySelectorAll() to querySelector().

It would also be advisable to add some code that checks if the document has fully loaded before running your code, otherwise the code could run before the qty selector has been added.

 

Me: I'm Paul, a SQSP user for >18 yrs & Circle Leader since 2017. I value honesty, transparency, diversity and good design ♥.
Work: Founder of SF.DIGITAL. We provide high quality original extensions to supercharge your Squarespace website. 
Content: Views and opinions are my own. Links in my posts may refer to my own SF.DIGITAL products or may be affiliate links.
Forum advice is completely free. You can thank me by selecting a feedback emoji. Buying a coffee is generous but optional.

Posted

Genius!  Thank you so much.  This worked:

 

<script>
        document.addEventListener('DOMContentLoaded', () => {
            var qty = document.querySelector('input[type="number"]');
              qty.setAttribute('max', '5'); 
        });
</script>

 

Create an account or sign in to comment

You need to be a member in order to leave a comment

×
×
  • Create New...

Squarespace Webinars

Free online sessions where you’ll learn the basics and refine your Squarespace skills.

Hire a Designer

Stand out online with the help of an experienced designer or developer.