vendredi 29 mai 2015

If/else statement: Search through multiple possible values only finds last value (javascript)

I have a form with various inputs to search an address. Only the province and postal code first appear when you load the page, but there is the option to open a toggle to display more options such as street name etc. Basically, what I'm trying to achieve is for the toggle to remain open when the page loads again if the select and text inputs are not empty ('' and 0).

I have most of the logic down, but my problem is that my if/else statement is only looking at the last value it is getting.

var allInputsValue;
var allInputs; //for testing purposes

$('.address-options').each(
    function(){
        allInputs = $(this); //for testing purposes
        allInputsValue = $(this).val();
        //console.log('Type: ' + allInputs.attr('type') + ' Name: ' + allInputs.attr('name') + ' Value: ' + allInputs.val());
        //console.log(' ID: ' + allInputs.attr('id') + '\n' + ' Value: ' + allInputs.val());
        console.log('Value: ' + allInputsValue);
    }
);

//if($('#civicNo').val() == '', $('#streetName').val() == ''){
if(allInputsValue === '' || allInputsValue === 0){
    alert("Empty fields");
    if (localStorage) {
        localStorage.setItem('addressOptions', 'closed');
    }
    togglePane("closed");
}else{
    alert("Fields not empty");

    if (localStorage) {
        localStorage.setItem('addressOptions', 'open');
    }
    togglePane("open");
}

I'm trying to keep the code fairly clean without having to do an else if for every input ID.

In the console log I get back all the correct values I am looking for. What am I missing?

Aucun commentaire:

Enregistrer un commentaire