0

I have a function which gets values from elements:

function getTransactionValues() {
    var o = {};
    o.reservations        = [];
    $('#custom-headers option:selected').each(function (i, selected) {
        o.reservations[i] = $(selected).val();
    });
    o.amount              = $('input[name=amount-price]').val();
    o.currency_value      = $('input[name=currency-value]').val();
    o.currency_name       = $('.currency_appendto option:selected').html();
    o.actual_amount       = $('input[name=actual-amount]').val();
    o.actual_remaining    = $('input[name=actual-remaining]').val();
    o.funds_arrival_date  = $('input[name=funds-arrival]').val();
    o.paid_to             = $('.paidto option:selected').html();
    o.checkbox            = $('.multi-transaction:checked').map(function () {
        return this.value
    }).get();
    return o;
}

Now i want to check whether amount, actual_amount and funds_arrival_date are filled. if they are i will release the disabled class from a button. i've tried

    var check_review = function () {
    var a = getTransactionValues();
    var options = [a.amount, a.actual_amount, a.funds_arrival_date];

    for(i = 0; i < options.length; i++) {
        if(options[i].length > 0) {
            $('a[name=review_button]').removeClass('disabled');
        }
        else{
            //this array is empty
            alert('There is a problem!');
        }
    }
}

$('.test').click(function() {
    check_review();
});

But it doesn't seems to be working..

2
  • Isn't it removing the class? Commented Nov 30, 2015 at 11:07
  • @void it removes if the elements inside the array have or does not have values.. like it doesn't check it right Commented Nov 30, 2015 at 11:08

3 Answers 3

1

Remove disabled attribute using JQuery?

Can you please look at above link, I think we should use $('.inputDisabled').prop("disabled", false);

Sign up to request clarification or add additional context in comments.

Comments

0

Even if a single array element will be non empty then your code will remove the class disabled from the a. To make sure that all the elements of array are non empty and then only you want to remove the class then the way is:

for(i = 0; i < options.length; i++) {
        if(options[i].length > 0) {
            $('a[name=review_button]').removeClass('disabled');
        }
        else{
            $('a[name=review_button]').addClass('disabled');
        }
    }

Or the other way is

var check = true;
for(i = 0; i < options.length; i++) {
        if(options[i].length == 0) {
            check = false;
        }

    }

if(check ) $('a[name=review_button]').removeClass('disabled');

Comments

0

Try using Array.prototype.every()

if (options.every(Boolean)) {
  $("a[name=review_button]").removeClass("disabled");
} else {
  // do other stuff
}

2 Comments

Could you tell me what does Boolean expect?
@IlanHasanov Empty string "" returns false ; e.g., Boolean("") // false , Boolean(".") // true

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.