0

I've looked through numerous topics regarding this, but none of the solutions are fitting my need. I need to enable the submit button only if there is data going to be submitted, and disable it again if there is no data.

                    $("input").each(function() {
                        $(this).keyup(function() {
                            console.log("keyup event fired");
                            $("input").each(function() {
                                if ( $(this).val() !== "" ) {
                                    empty = false;
                                } else {
                                    empty = true;
                                }
                            });
                            if ( empty ) {
                                $("#download-project").addClass("isDisabled");
                                $("#download-project").click(function(e) {
                                    e.preventDefault();
                                });
                            } else {
                                $("#download-project").removeClass("isDisabled");
                                $("#download-project").click(function(e) {
                                    e.preventDefault();
                                    $(".editor-form").submit();
                                });
                            }
                        });
                    });

My current code only enables the button, and than the if the user deletes the data, it doesn't disable the button. Looking at the console it also seems keyup is only firing once.

JSFiddle

8
  • 1
    you're never setting empty to true anywhere. Set it as true before your input each loop, if it stays true your if statement should handle the rest. Commented May 8, 2020 at 21:34
  • where is the HTML/CSS code? Commented May 8, 2020 at 21:36
  • Does this answer your question? Detect backspace and del on "input" event? Commented May 8, 2020 at 21:39
  • You're right that there should be a true declaration. I actually removed that trying to figure out why it wouldn't enable, and forgot to re-add it. But if it is added, the button will not enable. Commented May 8, 2020 at 21:39
  • @devlincarnate to be clear, that is the only way to detect if a value is than empty, even when supposedly cycling the values on each keypress? Commented May 8, 2020 at 21:42

2 Answers 2

1

You can add an input event listener to each of the input tags, which will fire when the value has been changed, and check if any of the input tags has a value that is only spaces.

const inputs = $('input'), downloadBtn = $('#download-project');
inputs.on('input', function(e){
  var invalid = inputs.is(function(index, element){
      return !$(element).val().trim();
  });
  if(invalid){
      downloadBtn.addClass("isDisabled").prop("disabled", true);
  } else {
      downloadBtn.removeClass("isDisabled").prop("disabled", false);
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form>
First Name: <input type="text"/><br/>
Last Name: <input type="text"/><br/>
Age: <input type="number"/><br/>
<button id="download-project" type="submit" disabled>
Submit
</button>
</form>

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

8 Comments

Thanks a bunch, I think this may be the solution. Though I'm having trouble copying the code. every time I select it, the whole fiddle frame is reloading.... I'll just write it out.
@WASasquatch No problem! I also put the code into a JSFiddle: jsfiddle.net/gy1dc9nx
thanks for the code. I tried to implement it, but it seems to only return false when I edit any inputs. I'm not sure why as you code is a little beyond my logic understanding.
@WASasquatch Could you provide an example where it does not work?
@WASasquatch One of your inputs appears to be hidden somehow.
|
0

Try to put change event listener instead of keyup.

$("input#submit").prop("disabled", true);
let isOk = true;
$("input").change(function (){
    $("input").each(()=>{
        console.log(this); 
        if($(this).val() === "") {
           isOk = false;
        } 
    });
    if(isOk) $("input#submit").prop("disabled", false);
})

Test: https://codepen.io/Opalecky/pen/xxwWmyJ

Comments

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.