Administrators Nathan Posted December 9, 2013 Administrators Share Posted December 9, 2013 I have the following code on my page that I need to apply some filters to: <script type="text/javascript"> function check() { var elements = document.getElementsByName("quantity"); var qtycheck = document.getElementById("qtycheck").value; for (var i=0; i<elements.length; i++) { var nmbr = elements[i].value; if (nmbr > <?php echo $maxqtyorder; ?>){ alert("You've selected more treats than your package allows."); return false; } else if (nmbr > qtycheck){ alert("You've selected more treats than your package allows."); return false; } } return true; } </script> The 2nd line: var elements = document.getElementsByName("quantity"); I want to get all the elements by name of quantity but exclude the ones that have an ID of quantity2442 and quantity2443.Is there a way to do this when setting the variable? Quote Link to comment Share on other sites More sharing options...
Administrators Nathan Posted December 9, 2013 Author Administrators Share Posted December 9, 2013 Here is the finished solution. function check() { var elements = document.getElementsByName("quantity"); var qtycheck = document.getElementById("qtycheck").value; for (var i=0; i<elements.length; i++) { if (elements[i].id != "quantity2442" && elements[i].id != "quantity2443") { var nmbr = parseInt(elements[i].value,10); if (nmbr > 100){ alert("You've selected more treats than your package allows." + nmbr); return false; } else if (nmbr > qtycheck){ alert("You've selected more treats than your package allows." + nmbr); return false; } } } return true; } Important notice: you will have to add the parseInt to var nmbr = parseInt(elements[i].value,10); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.