Jamz Posted May 19, 2022 Posted May 19, 2022 (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 May 19, 2022 by Jamz
Solution paul2009 Posted May 19, 2022 Solution Posted May 19, 2022 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.
Jamz Posted May 19, 2022 Author Posted May 19, 2022 Genius! Thank you so much. This worked: <script> document.addEventListener('DOMContentLoaded', () => { var qty = document.querySelector('input[type="number"]'); qty.setAttribute('max', '5'); }); </script> paul2009 1
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment