4

I have this Fiddle and i made the script to work when the check box is checked first and than text inserted in input,but if i try to insert text first button wont enable it self. Can someone look at that fiddle? this is my script

$('.checkset label').on('change', function () {
var reqlength = $('.important').length;
console.log(reqlength);
var value = $('.important').filter(function () {
    return this.value != '';
});


if ($('.checkset label').hasClass("checked")) {
    if (value.length >= 0 && (value.length !== reqlength)) {
        //alert('Please fill out all required fields.');
        $('#reg').prop('disabled', true);
        $('#reg').addClass('inactive');
    } else {
        //alert('Everything has a value.');
        $('#reg').prop('disabled', false);
        $('#reg').removeClass('inactive');
    }
} else {
    $('#reg').prop('disabled', true);
    $('#reg').addClass('inactive');
}
});
   $(".important").on('change', function () {
   $('.checkset label').change();
});

1 Answer 1

1

Try substituting input element for label element at change events , utilizing .toggleClass() to set .checked class at on .checkset label if all .important input elements have, do not value

$(".checkset").click(function(){
  $(this).find("label").toggleClass("checked"); 
});
var res;
var inputs = $(".important, .checkset input");
inputs.on("input change", function() {
  res = inputs.get().every(function(el) {

    return $(el).is("[type=checkbox]") ? el.checked : el.value.length !== 0 
  });
    $("#reg").prop("disabled", !res)
    .toggleClass("inactive", !res)
})

jsfiddle http://jsfiddle.net/r9rsddhk/15/

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

11 Comments

Still works the same. i need the script to make button enabled when text is inserted and checkbox is checked. for now the script works only if checkbox is checked first.
@DejanJankov Is requirement that all input elements have a value before button is enabled ?
all with class important + checkbox checked. for now i have checkbox checked + all with class important.
i don't need it to work for all input,just the input with class important
@DejanJankov Try changing selector to var inputs = $(".important") jsfiddle.net/r9rsddhk/6
|

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.