Javascript Filter on Variable Set

Discussion in 'JavaScript' started by gallitin, Dec 6, 2013.

  1. #1
    <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>
    Code (markup):
    The 2nd line:
    var elements = document.getElementsByName("quantity");
    Code (markup):
    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?
     
    Solved! View solution.
    gallitin, Dec 6, 2013 IP
  2. #2
    Generally speaking no, and any processing to check them before the loop would take as long as inside the loop -- I'd suggest using a switch for the ID inside the loop that does a 'continue', skipping the rest of the code in the loop and proceeding to the next item.

    Some advice: if both test cases do the same thing, don't make them separate test cases -- outputting the entire script in PHP means it can't be cached, have PHP assign that value to a global or something... and if you have a bunch of VAR in a row you can make them a comma delimited list, so you don't have to say "VAR" every blasted time.

    function check() {
    	var
    		elements = document.getElementsByName("quantity"),
    		qtycheck = document.getElementById("qtycheck").value,
    		i, value;
    
    	for (i = 0; i < elements.length; i++) {
    		switch (elements[i].id) {
    			case 'quantity2442':
    			case 'quantity2443':
    				continue;
    		}
    		value = elements[i].value;
    		if (
    			(value > <?php echo $maxqtyorder; ?>) ||
    			(value > qtycheck)
    		) {
    			alert("You've selected more treats than your package allows.");
    			return false;
    		}
    	}
    	return true;
    }
    Code (markup):
    another approach would be to put the ones you don't want in an array and use Array.indexOf, but you'd want to include a shim since older browsers (IE8/earlier) don't have that method.

    if (!Array.indexOf) Array.prototype.indexOf = function (v, s) {
    	if (this == null || typeof this == 'undefined') throw new TypeError(
    		'Illegal operation on undefined variable'
    	);
    	for (var t = s || 0; t < this.length; t++) if (this[t] == v) return t;
    	return -1;
    };
    
    function check() {
    	var
    		elements = document.getElementsByName('quantity'),
    		qtycheck = document.getElementById('qtycheck').value,
    		blackList = ['quantity2442', 'quantity2443'],
    		i, value;
    
    	for (i = 0; i < elements.length; i++) {
    		if (blackList.indexOf(elements[i].id) >= 0) continue;
    		value = elements[i].value;
    		if (
    			(value > <?php echo $maxqtyorder; ?>) ||
    			(value > qtycheck)
    		) {
    			alert("You've selected more treats than your package allows.");
    			return false;
    		}
    	}
    	return true;
    }
    Code (markup):
    The blackList is probably easier if you have a lot of them, especially if you're going to want PHP to set them using a global or value pass so the script itself can be cached across pages.
     
    deathshadow, Dec 6, 2013 IP
  3. gallitin

    gallitin Well-Known Member

    Messages:
    738
    Likes Received:
    45
    Best Answers:
    2
    Trophy Points:
    165
    #3
    Exactly what I was looking for thanks!
     
    gallitin, Dec 7, 2013 IP