Solved

Javascript Filter on Variable Set

Posted on 2013-12-06
1
330 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

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 the s#!t hits the fan, you don’t have time to look up who’s on call, draft emails, call collaborators, or send text messages. An instant chat window is definitely the way to go, especially one like HipChat. HipChat is a true business app. An…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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).

691 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