Solved

Javascript Filter on Variable Set

Posted on 2013-12-06
1
319 Views
Last Modified: 2013-12-06
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>

Open in new window


The 2nd line:
var elements = document.getElementsByName("quantity");

Open in new window


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?
0
Comment
Question by:N R
1 Comment
 
LVL 44

Accepted Solution

by:
Rainer Jeschor earned 500 total points
ID: 39701766
Hi,

here we go:
    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;
    }

Open in new window


Important notice: you will have to add the parseInt to
 var nmbr = parseInt(elements[i].value,10);

Open in new window

otherwise it might be treated as text and e.g. "2" is greater than "100".

Online sample:
http://jsfiddle.net/EE_RainerJ/FPvsW/

HTH
Rainer
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now