Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a simple drop-down select menu with HTML. When I click an option, its value gets added to an array called exceptions. I want to make sure the option value does not already exist in the array exceptions, and if it does cancel the entire function with return. It is all working except the part of function loop() that compares each element of array exceptions to the option clicked.

exceptions = new Array();

$(document).ready(function() {     

    $('select').click(function() {
       var clicked = $(this).val();
       loop(exceptions,clicked);
       exceptions.push( clicked );
    }); 

});

function loop(array,clicked) {
    for (var i=0;i<array.length;i++) { 
        if (array[i] == clicked) {return;}
    }
}
share|improve this question

1 Answer

You can do:

$('select').click(function() {
   var clicked = $(this).val();
   loop(exceptions, clicked) ? exceptions.push(clicked) : return;
});

function loop(array,clicked) {
    for (var i=0;i<array.length;i++) { 
        if (array[i] == clicked) {return false;}
    }
    return true;
}
share|improve this answer
still not working,but thanks. I'm assuming that 'clicked' and array[elements] are all strings, but maybe not, and that's why there not comparing... – seamus yesterday

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.