how can i access the codeisbn variable from outside the jquery keydown function

You want to use keyup, rather than keydown. Assuming the ISBN number should be greater than 10 characters this should work for you. You will want to probably do something different to validate the isbn format and length. This isn’t a perfect solution ready for production, but it’s a step in the right direction.

$(function(){
   let codeIsbn;
   $('#bookISBN').keyup(function(){
     if( $(this).val().length > 9 ) { 
       let bookIsbn = $('#bookISBN').val();
       let splitISBN = bookIsbn.split('-');
       codeIsbn = splitISBN[0]+'-'+splitISBN[1]+'-'+splitISBN[2];
       if( codeIsbn.length > 9 ) { 
           $('#bookCode').val(codeIsbn);
       }
     }
   });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="row">
   <div class="col-lg-6 col-sm-12">
      <div class="form-group">
         <label for="">ISBN Buku</label>
         <input type="text" name="isbnBuku" class="form-control" id="bookISBN" data-inputmask="'mask': ['999-999-999-99-9']" data-mask placeholder="ISBN Buku">
      </div>
   </div>
   <div class="col-lg-6 col-sm-12">
      <div class="form-group">
         <label for="">Kode Buku</label>
         <input type="text" name="kodeBuku" id="bookCode" class="form-control" placeholder="Kode Buku">
      </div>
   </div>
</div>

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top