Jump to content

Limit Product Qty on Product

Go to solution Solved by paul2009,

Recommended Posts

 

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
Link to comment
  • Solution
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.

 

Improve your online store with our extensions.
About: Squarespace Circle Leader since 2017. I value honesty, transparency, appreciation and great design ♥.
Work: Squarespace Developer and founder of SF Digital, building the features Squarespace didn't include™.
Content: Links in my posts may refer to SF Digital products or may be affiliate links.

Buy me a coffee

Link to comment

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.