Search code examples
jqueryeachis-empty

How to check if all input values are empty in jquery each?


I have an each and need to check if all input fields are empty and do some action. SO i did the following

jQuery(".page-template-sitecheckout .mainwrapper .shipping_addresses_checkout_view.checkout_shipping_column_withoutmargin .form-row.validate-required input").each(function () {
        var checkoutfieldsvalue = jQuery(this).val();
        if(checkoutfieldsvalue === ''){
            jQuery(this).parent().addClass('checkout_invalid_fields_parent');
            jQuery(this).addClass('checkout_invalid_fields');
        }else{
          jQuery(this).parent().removeClass('checkout_invalid_fields_parent');
            jQuery(this).removeClass('checkout_invalid_fields');
            jQuery('.checkout_btn_prev_to_shipping').fadeIn().show();
            jQuery('.page-template-sitecheckout .mainwrapper .shipping_addresses_checkout_view').fadeOut().hide();
            jQuery('.page-template-sitecheckout .mainwrapper .billing_info_checkout_view').fadeIn().show();
            jQuery('.checkout_btn_prev_to_cart, .checkout_btn_next_to_billing').fadeOut().hide();
        }
    });

With this code when even one input has a value it passes to else statement. So how i can check for every single input field value emptiness and do some action?

Thanks in advance.


Solution

  • I think this is what you are expecting.

    var count=0;
    $('.page-template-sitecheckout .mainwrapper .shipping_addresses_checkout_view.checkout_shipping_column_withoutmargin .form-row.validate-required input').each(function(){
    if($(this).val == ""){
    count++;
    }
    });
    if(count == 0)
    {
    //do something here
    }