Jump to content
Sign in to follow this  
Nathan

Javascript Count Quantities

Recommended Posts

<form action="/proc" method="post" onsubmit="return check();">
                <input type="hidden" name="twelve" value="399">
                <div class="quantity">
                    <input type='button' value='-' class='qtyminus' field='quantity12' />
                    <input type='text' id='quantity12' value='0' class='qty' name="quantity12"/>
                    <input type='button' value='+' class='qtyplus' field='quantity12' />
                </div>

</div>
            <div style="width:300px;float:left;margin-left:30px;">
                    <input type="hidden" name="twentyfour" value="547">
                        <div class="quantity">
                            <input type='button' value='-' class='qtyminus' field='quantity11' />
                            <input type='text' id='quantity11' value='0' class='qty' name="quantity11"/>
                            <input type='button' value='+' class='qtyplus' field='quantity11' />
                        </div>
                </div>
            <div id="clearfix"></div>
                        <div style="margin-top:175px;"><input type="submit"/></div>
            </form> 

Javascript:

<script type="text/javascript">
        function check() {
            var elements = document.getElementsByName("quantity11");
            var elements1 = document.getElementsByName("quantity12");
            for (var i=0; i<elements.length; i++) {
                    var nmbr = parseInt(elements[i].value,10);
                    var nmbr1 = parseInt(elements1[i].value,10);
                    if ((nmbr*2) + nmbr1 > <?php echo $maxqtyorder; ?>){
                        alert("You've selected more treats than your package allows.");
                        return false;
                    }
                }
            return true;
        }
    </script> 

I have a page that includes 2 different options to select. You can select the quantity before you add to your cart.  I'm trying to make sure that the quantity the customer selects is not more than a certain total I've already passed to that page.  After about an hour of trial and error it worked!

 

HTML:

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...