2

I'm using .inArray() to check my array for a '0' value and then return false; or break the function if there is a zero.

Here's the code I'm currently using:

$(document).ready(function() {

    $('#calc').click(function(e) {

        var drop = $(".drop"),
            c = [],
            g = [];

        //Push values into an array.
        drop.each(function() {
            var $t = $(this),
                cals = $t.val(),
                option = $(':selected', this),
                gly = parseFloat(option.attr('rel'));
            g.push(gly * 2);
            c.push(cals * 2);
        });

        var inthere = jQuery.inArray('0', c) > -1;

        //don't calculate if array is empty
        if (c.length === 0) {
            return false;
        }
        //don't calculate with a '0' value in array
        if (inthere == true) {
            return false;
        }

        //shouldn't display if you haven't added a dropdown OR if the dropdown stayed on the "Default" option
        alert('Passed');
    });

    $('#add').click(function() {
        $('#contain').append('<select class="drop"><option value="" rel="" data-default="true">Default</option><option value="1" rel="2">Option 1</option><option value="3" rel="4">Option 2</option></select>');
    });

});​

When the user leaves the option at the "Default" option, the value passed to c should be 0. For some reason, the code will pass, unless I haven't added in a dropdown...

How can I prevent the code from continuing if there is a '0' in the c array???

Here's the fiddle

1 Answer 1

2

The array doesn't have a string matching '0', it has a number matching 0.

http://jsfiddle.net/DUs8e/1/

var inthere = jQuery.inArray(0, c) > -1;
Sign up to request clarification or add additional context in comments.

1 Comment

I figured this out 3 seconds before you posted it! haha, I was going to ask why, but you answered that also, thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.