Anahit DEV Anahit DEV - 1 year ago 118
jQuery Question

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('.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.

Answer Source

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 == ""){
if(count == 0)
//do something here
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download