Solved

Javascript Filter on Variable Set

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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

820 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