0

I have a form which consists of some elements such as a select-input and a checkbox.

The submit-button is disabled and I want to enable the button only if two conditions are fulfilled. An initial version works well, but only if clicking on the checkbox is the last step. But it should be a function that reacts on both, clicks/changes in the select and the checkbox.

The following code is working but with the problem explained above.

$('#toscheck').click(function() {
    if ($(this).is(':checked') && $("#ctry").val().length > 0) {
      $('#pjo').removeAttr('disabled');
      $('#sjo').removeAttr('disabled');
    } else {
      $('#pjo').attr('disabled', 'disabled');
      $('#sjo').attr('disabled', 'disabled');
    }
});

The following solution doesn't work:

$('document').ready(function() {
    if ($('#toscheck').is(':checked') && $("#ctry").val().length > 0) {
      $('#pjo').removeAttr('disabled');
      $('#sjo').removeAttr('disabled');
    } else {
      $('#pjo').attr('disabled', 'disabled');
      $('#sjo').attr('disabled', 'disabled');
    }
});

But how can I solve this? What I have found on SO wasn't really helpful.

Again: it should work as following; if the checkbox is selected AND the selected option has a value, the button would be enabled.

Thanks in advance.

4
  • Please include your html Commented May 18, 2018 at 15:30
  • $(#toscheck) -> $('#toscheck'). Voting to close as a typo. Also note that you can combine selectors: $('#pjo, #sjo').removeAttr('disabled');. I'd also suggest using prop() over attr(). Then you can so .prop('disabled', true) or false as needed Commented May 18, 2018 at 15:30
  • The solution had the '. This was not the problem. I had them in the code. Deleted this and entered from my mind. Commented May 18, 2018 at 15:33
  • @rory the problem described by the OP is not related to the typo. Commented May 18, 2018 at 15:35

2 Answers 2

2

First, store you element in variables:

let $toscheck = $('#toscheck'),
    $ctry = $("#ctry"),
    $pjo = $('#pjo'),
    $sjo = $('#sjo');

Then, create your validation function with the stored variables. Note that I replace attr and removeAttr with .prop, it is better:

function checkThings(){
    if ($toscheck.is(':checked') && $ctry.val().length > 0) {
        $pjo.prop('disabled', false);
        $sjo.prop('disabled', false);
    } else {
        $pjo.prop('disabled', true);
        $sjo.prop('disabled', true);
    }
}

Then, bind the events:

$toscheck.add($ctry).on( 'change', checkThings );

Note that I used change on both elements since it does work with inputs and checkboxes.


Final code :

let $toscheck = $('#toscheck'),
    $ctry = $("#ctry"),
    $pjo = $('#pjo'),
    $sjo = $('#sjo');

function checkThings(){
    if ($toscheck.is(':checked') && $ctry.val().length > 0) {
        $pjo.prop('disabled', false);
        $sjo.prop('disabled', false);
    } else {
        $pjo.prop('disabled', true);
        $sjo.prop('disabled', true);
    }
}

$toscheck.add($ctry).on( 'change', checkThings );
Sign up to request clarification or add additional context in comments.

1 Comment

Works even a bit better than Avi's solution. Thanks.
1
$(document).ready(function() {
$('#toscheck,#ctry').change(function() {
    if ($('#toscheck').is(':checked') && $("#ctry").val().length > 0) {
  $('#pjo').removeAttr('disabled');
  $('#sjo').removeAttr('disabled');
} else {
  $('#pjo').attr('disabled', 'disabled');
  $('#sjo').attr('disabled', 'disabled');
}
});

});

use this code .change function detects change and then on call check whether your AND condition is met or not

and add #toscheck in quotes i.e. '#toscheck'

$('#toscheck,#xyz,#abc').change()

for detecting change for multiple elements

3 Comments

As soon as the six minutes are over which SO shows me I will accept your answer. :-) As for the quotes, I got them in my initial second solution which I then deleted as it didn't work. But you are right.
FYI, document doesn't need quotes. It forces jQuery to do a querySelectorAll which returns nothing and is not needed.
here it is was a quick copy paste type :). @nucky you could also use multiple elements in .change function

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.