1

My alert divs don't show up when I click the submit button.

An 'Error' div should alert when there's an empty required field and, a 'Success' div should alert right before the form submits. The form submits so I know the validation check works but, I don't see any of my alert divs. See code below:

const goaForm = document.getElementById('goa-form');
let formCompleted = $.trim($('#goa-form input[required]').val()) == !'';
let formIncomplete = $.trim($('#goa-form input[required]').val()) == '';
let success = document.getElementById('success-msg');
let error = document.getElementById('error-msg');

let submitButton = document.getElementById("btnSubmit");

function checkForm() {
    if (formCompleted) {
        success.style.visibility = 'visible';
        goaForm.submit();
    } else if (formIncomplete) {
        error.style.visibility = 'visible';
        $("#error-msg").fadeOut(28000);
        return false;
    }
}
submitButton.addEventListener("click", checkForm);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="error-msg" style="visibility: hidden;" class="alert alert-danger" role="alert">
<span class="fs-14">Error message div</span></div>

<div id="success-msg" style="visibility: hidden;" class="alert alert-success" role="alert">
<span class="fs-15">Success!</span></div>

// Submit Button
<button onclick="checkForm()" id="btnSubmit" class="btn btn-success lift d-flex align-items- 
 center" type="submit">Submit my application <i class="fe fe-arrow-right ml-5"></i>
</button>

Thanks for the help guys.

Poul Bak
  • 10,450
  • 5
  • 32
  • 57
Argo Lith
  • 37
  • 4

1 Answers1

1

checkForm() is fired when your button is clicked, but it uses values (formCompleted, formIncomplete) defined earlier, on page load. So you may fill out your form, but those variables are not updated, and clicking the button uses the old values that were set when the page was first loaded.

Instead, check the input states and define those variables inside your checkForm() function, so that they are set up as the actual, current results when the button is clicked. Then the tests evaluate what the form looks like at the time of the button click.

function checkForm() {
    let formCompleted = $.trim($('#goa-form input[required]').val()) == !'';
    let formIncomplete = $.trim($('#goa-form input[required]').val()) == '';
    // ... rest of your code ...

Update

Here's a working JSFiddle.

Notes:

  • You're using a mix of plain JS and jQuery. There's nothing technically wrong with that, but it would certainly be easier to read and maintain if you stuck to one. If you are going to take the hit in loading jQuery (extra http request, 90kb odd extra resource, etc) you may as well use it.

  • I am not sure if it is actually invalid, but the formCompleted test seems wrong. I'd use the standard !== '' instead of == !'' (I've done that in the JSFiddle);

  • If you're going to use the type comparison for formCompleted, you should be consistent and also use it for formIncomplete, ie use === '' (I've done that in the JSFiddle);

  • Don't use both an inline onClick() on the button, and add an addEventListener in your JS. They both do the same thing, use only one of them. It is considered bad practice to mix JS in with your HTML, so using the plain JS addEventListener (or jQuery .on()) is better. I've removed the inline one from the JSFiddle.

Don't Panic
  • 13,965
  • 5
  • 32
  • 51
  • Hey Panic. It works in jsFiddle but not in my local environment. This is the message I got in my console: jQuery.Deferred exception: Cannot read property 'addEventListener' of null TypeError: Cannot read property 'addEventListener' of null at HTMLDocument. – Argo Lith Apr 22 '20 at 01:56
  • Sounds like a few problems in your code - did you copy-paste the JSFiddle? 1) The error is telling you that `submitButton`, which should represent your button, is actually null. That means it is either not defined, or is set to an element which does not exist. Double-check how you define `submitButton`, make sure the button has the ID you are using, etc. 2) This particular line of code, with `addEventListener`, is not jQuery, and has nothing to do with `Deferred` - you have another problem somewhere. Try removing/commenting out things until you get it working. – Don't Panic Apr 22 '20 at 02:31
  • I did copy the fiddle. Still didn't work. No issues in the console. I'm gonna take a different route and I'll let you know how it turns out. – Argo Lith Apr 22 '20 at 17:11
  • I'm not sure what you mean now. Your previous comment described an error on the console. – Don't Panic Apr 22 '20 at 23:38
  • Hi Panic...I hope you're still here. I solved that first problem but now I have another issue. I implemented some user feedback for users. If required fields are empty, some error messages will appear to alert users that the inputs are empty. I have been working all weekend on this. I get the correct feedback for empty 'text' values. I'm having a hard time checking for empty checkbox and radio inputs. By default, when you click submit, I get the correct error message for empty text fields but not radio and checkbox items (filled by default) See: https://jsfiddle.net/krisixco/zgdf2sec/3/ – Argo Lith Apr 26 '20 at 18:28
  • Hi @ArgoLith - if my answer helped, [please accept and upvote it](https://stackoverflow.com/help/someone-answers), thanks. If you have a new question, post a new question! :-) – Don't Panic Apr 26 '20 at 23:31