-1

long time reader and have used this site for help for years. Most of my problems I have been able to solve using this site but this one is killing me as it is delaying the release to my customer (which should have been done a month ago).

This isn't the exact code but should show what I am looking to do. My problem is that when a condition isn't met after clicking the "Submit" button it is clearing all of the fields rather than allowing the user to continue off where they were.

Example code:

$(document).on('click','#btngetinfo',function(e){
            var gettheinfo = 0;
            e.stopImmediatePropagation();
            var weight1 = $("#txtweight1").val();
            var length1 = $("#txtlength1").val();
            var width1 = $("#txtwidth1").val();
            var height1 = $("#txtheight1").val();
             //Conditional stuff thrown in here
               if (height1 != 0 && height1 != "0" && height1 != "" && height1 < 98) {
                gettheinfo = 1;
               }

            if (gettheinfo == 1){
               //Run AJAX code
               console.log("Variables:", weight1, length1, width1, height1);
            }

              
        })
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" id="txtweight1" class="form-control">
    <input type="text" id="txtlength1" class="form-control">
    <input type="text" id="txtwidth1" class="form-control">
    <input type="text" id="txtheight1" class="form-control">
    <button type="button" class="btn gflbtn btn-block" id="btngetinfo">Submit</button>

Any help would be appreciated because I am losing hours and hours on this. Thank you in advance!

robere2
  • 1,689
  • 2
  • 16
  • 26
  • What do you mean by throw message? – Darren McCarthy Dec 11 '18 at 20:02
  • Everything else seems to act as expected and depending on input I receive the correct responses in the alert(). I've also tried with `preventDefault()` and `return` or `return false` – Darren McCarthy Dec 11 '18 at 20:04
  • add this to your button type="button". I suspect the button is submitting a form. – Nawed Khan Dec 11 '18 at 20:07
  • Sorry, Nawed.. I do have that in my original code but neglected to type it out in this example. I edited my post to include it. – Darren McCarthy Dec 11 '18 at 20:09
  • https://stackoverflow.com/questions/8701812/clear-form-after-submission-with-jquery check this – demo Dec 11 '18 at 20:15
  • 1
    Please clarify, you want the input in the fields to remain in case of error, correct? It is working fine for me, the input is not clearing upon clicking submit. If this is not what you want, then can you add more details? – robere2 Dec 11 '18 at 20:20
  • do you have other conditions along with checking for height1 variable? Also Side Note: I would prefer this for your Height variable check "if (Number(height1) > 0 && Number(height1) < 98)" – Karthik Ganesan Dec 11 '18 at 20:25
  • You are correct @robere2 but for some reason it does not appear to be working in this situation for me. This is an approach I've taken and executed many times before without issue. – Darren McCarthy Dec 11 '18 at 20:26
  • @demo I will look into that post. Thank you – Darren McCarthy Dec 11 '18 at 20:26
  • @DarrenMcCarthy Please provide more code if you can or there's anything you left out. The current code doesn't appear to reproduce your issue. Also perhaps try running on another browser. – robere2 Dec 11 '18 at 20:27
  • @KarthikGanesan There are other conditions being checked but they are similar to the height, only difference is length < 288 and width < 97 written out the same way. – Darren McCarthy Dec 11 '18 at 20:28
  • the current code, as presented in opening question, has no issues and working as expected. Then the issue must be somewhere else. – Nawed Khan Dec 11 '18 at 20:29
  • I think your "example code" is missing some key trait that your real code has which is causing the problem. If you can please show us a bit more of the HTML that builds up the form, that might help. – IceMetalPunk Dec 11 '18 at 20:35
  • Forms don't clear by themselves, and there's nothing in this code that would clear the form. – Barmar Dec 11 '18 at 20:36
  • hope your HTML is not wrapped within a FORM tag which is submitting ... Also is your If Else based on only 1 variable "gettheinfo" or do you have other variables per condition? – Karthik Ganesan Dec 11 '18 at 20:36
  • Thank you for the help everyone.. I am going to start the page piece by piece from scratch again and see if I can isolate it the issue. I agree with @IceMetalPunk that the issue has to be elsewhere but I just can't see it and I have too much sensitive data in the rest of the code to be able to provide the full real code. – Darren McCarthy Dec 11 '18 at 20:48

1 Answers1

0

Again, thank you to everyone that added to the discussion. The issue looks like it ended up being from using $(document).on('click','#btngetinfo',function(e){. This morning I attempted using $('#btngetinfo').click(function(e){ and everything is running as expected.

  • You're just pushing the problem on the side.. It's still there since you don't stop the normal form behavior. Not even mentioning that you can't perform what you're trying to do without the live version of $.click() – Richard Dec 13 '18 at 16:35