Jamz Posted May 19, 2022 Share 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 Link to comment
Solution paul2009 Posted May 19, 2022 Solution Share 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. About: Squarespace Circle Leader since 2017. I value honesty, transparency, diversity 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. Catch up on all the release notes and announcements 2023 [for Circle members only]. There's a public version here too!If I helped, you can thank me by clicking one of the emojis below. If you prefer, you can buy me a coffee.Improve your online store with our extensions. Link to comment
Jamz Posted May 19, 2022 Author Share 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 Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment