Solved

Javascript Filter on Variable Set

Posted on 2013-12-06
1
321 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Read about why website design really matters in today's demanding market.
"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

948 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

20 Experts available now in Live!

Get 1:1 Help Now