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
  • Replies 2
  • Views 243
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

  • 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.

 

About me: I'm Paul. A SQSP User for 18 yrs, I joined Circle when it launched in 2016 and have been a Circle Leader since 2017. I value honesty, transparency, diversity and good design ♥.
Work: Founder of SF.DIGITAL, providing expertise and 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 free. You can thank me by clicking one of the feedback emojis below. Coffee fuels my work.

Book paid help with a Squarespace Domain

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.