I want to add keyup and click on a button

Tags: javascript
Question!

whenever the user press or click the button with the #btnsubmit the ajax executes. but when i press enter. both keyup and click executes.
but when i use click. it only executes the click function.

<script type="text/javascript">
       function loaddelegates($barcode)
         {
            var xhttp = new XMLHttpRequest();
            xhttp.onreadystatechange = function() {
              if (this.readyState == 4 && this.status == 200) {
               document.getElementById("getdel").innerHTML = this.responseText;
               $("#btnsubmit").focus();
              }
            };
            xhttp.open("GET", "activityclient/" + $barcode, true);
            xhttp.send();
          }

       function storetime($barcode)
         {
            var xhttp = new XMLHttpRequest();
            xhttp.onreadystatechange = function() {
              if (this.readyState == 4 && this.status == 200) {
               document.getElementById("getdel").innerHTML = this.responseText;
               $("#barcode").focus().select();
              }
            };
            xhttp.open("GET", "activityclient/" + $barcode + "/store", true);
            xhttp.send();
          }

   $(function() {

         $(document).on('keyup','#barcode',function(e) {
              e.preventDefault();
              var code = e.keyCode || e.which;
              if (code == 13 && $(this).val().length >= 1) {
                  loaddelegates($(this).val());
                  return false;
              }
        });

              $(document).on('keyup','#btnsubmit',function(e) {
              e.preventDefault();
              var code = e.keyCode || e.which;
              if (code == 13) {
                 var barcode = document.getElementById('barcode').value;
                  storetime(barcode);
                  return false;
              }
        });

         $(document).on('click','#btnsubmit',function() {
                 var barcode = document.getElementById('barcode').value;
                  storetime(barcode);
                  return false;
        });
      });

thank you



Answers

You don't need to add an event handler for the enter key. Remove it and that should work.

button click event is triggered after keypress using jQuery

By : Passersby


This video can help you solving your question :)
By: admin