jQuery function that keeps a field active

Viewed 26

I created this little function jQuery that when we select an option it returns the value:

enter image description here

But when I click on another option above, the value disappears and I need to go back in the select field and select another option to appear again

enter image description here

how can i keep this effect on click without it disappearing like that? Thanks for reading this far!

My code that handles this field:

if ($('.wc-pao-addon-distribuicao select').val() == 0 || $('.wc-pao-addon-tamanho-da-empresa select').val() == 0 || $('select#formato').val() == 0) {
    $("#product-addons-total").hide();

  } else {
    $("#product-addons-total").show();

  }
});
1 Answers

I think, there is a problem with your logic. Your current logic defines that out of 3 fields if one of them is 0, you hide the total and when all are not 0 then only you show the total.

so whenever you choose all the values and they are not 0, the total is showing. but when you change an option, one of the values is reset to 0 and in this case, your logic definition for hide statement becomes true.

So, you need to understand and correct your logic.

What I am thinking here is that the logic should be that out of 3 fields if one of them is not 0 then show the total. so we'll define this condition by this code:

if (
    $('.wc-pao-addon-distribuicao select').val() != 0 ||
    $('.wc-pao-addon-tamanho-da-empresa select').val() != 0 ||
    $('select#formato').val() != 0
) {
    $('#product-addons-total').show();
} else {
    $('#product-addons-total').hide();
}
Related