0

I have this code:

    $('#inputID').on('keyup', function () {
      if ($('#inputID').val() == '') {
          $(".button").removeClass('uk-button-success');
      } else {
          $(".button").addClass('uk-button-success');
      }
    })

It requires a single input to be filled, so a specific button gets a class added. Now I want multiple inputs to be checked and if all of them are filled and not empty, I want to add the class "uk-button-success" to a button.

 if ($('#inputID', '#inputID2').val() == '')

and other stuff I've tried didn't work.

How do I do this?

7
  • 1
    You might try to use a logical OR operator: if ($('#inputID').val() == '' || $('#inputID2').val() == '' || ...) { /* some of them is empty */} else {/* all are filled */} Commented Apr 21, 2021 at 13:58
  • This works, nice! How do I get the multiple IDs now with the keyup function? $('#inputID1', '#inputID2').on('keyup', function () { doesn't work. Commented Apr 21, 2021 at 14:10
  • @IhorVyspiansky there is a better way, see below ;) Commented Apr 21, 2021 at 14:24
  • @Jonas Set, for ex., the same class name for all inputs. Let it be my-input. And then you may use $('.my-input').on('keyup', function () {...? Commented Apr 21, 2021 at 14:59
  • @StéphaneM As fas as I understand, it allows to check clicked input value only ($(this).val()), but not other inputs (quote: I want multiple inputs to be checked and if all of them are filled and not empty). Commented Apr 21, 2021 at 15:11

1 Answer 1

1

Just target by class to have multiple element and then use $(this)

Eg: On a batch of input to disable (or not) submiting following all values.

$(function() {
  $('.input').on('keyup', function() {
    result = true
    $('.input').each(function() {
      if ($(this).val() === '') {
        result = false
      }
    })
    result ? $("#target").prop('disabled', false) : $("#target").prop('disabled', true);
  });
});

https://jsfiddle.net/g84p5b6v/

Your base use case

$(function() {
    $('.input').on('click', function () {
      if ($(this).val() === '') {
          $("#target").removeClass('success');
      } else {
          $("#target").addClass('success');
      }
    })
});

=> https://jsfiddle.net/45of2nrk/

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.