Search code examples
javascriptjqueryinputkeypresskeyup

Auto resize text element not working with keyup


In my below jQuery I have an element that reflects the written input within a secondary element. At the same time the element with the reflected text needs to resize, this is working, but there are 2 problems I cannot resolve:

1. When holding a backspace down, it doesn't keep up.
2. When you press single backspace, it skips a character and field cannot be emptied.

Please see below snippet:

jQuery(document).ready( function() {
    jQuery(function(){
        jQuery('#vanity-span').text(jQuery('#reflection').val());
        jQuery('#reflection').width(jQuery('span').width());
    }).on('input', function () {
        jQuery('#vanity-span').text(jQuery('#reflection').val());
        jQuery('#reflection').width(jQuery('span').width());
    });
    jQuery('#vanity-url').bind('keypress keyup blur', function() {
        jQuery('#reflection').val(jQuery(this).val());
    });
});
body {
  background-color: #e4e4e4;
  font-family: Arial;
}

#vanity-url-wrapper {
  margin-top: 3em;
  text-align: center;
}

#vanity-url-wrapper > span {
  background-color: #FBE3CF;
  border-radius: 8px;
  padding: 0.5em 0;
  border: 2px solid orange;
}

.pre-span {
  background-color: orange;
  color: white;
  font-weight: bold;
  padding: 0.5em;
}

#vanity-url {
  display: block;
  text-align: center;
  width: 12em;
  margin: 3em auto;
  font-size: 1.2em;
  border-radius: 5px;
  border: 2px solid orange;
  padding: 0.5em;
}

#vanity-span{
  padding: 0.5em;
}

#reflection {
  display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<body>
  <div id="vanity-url-wrapper">
    <span>
      <span class="pre-span">https://example.com/</span>   
      <span id="vanity-span"></span>
      <input id="reflection" type="text" readonly>
      <span class="pre-span">/</span>
    </span>
  </div>
  <input id="vanity-url" type="text" placeholder="Type here your vanity URL">
</body>


Solution

  • The problem is that the .bind('keypress keyup blur', function() { is not cooping well with updating the values. When the key is down it needs an update and is waiting for up, it then skips, and vice versa.

    So the solution here is to use .on('input', function() { instead.

    See below outcome:

    jQuery(document).ready( function() {
        jQuery(function(){
            jQuery('#vanity-span').text(jQuery('#reflection').val());
            jQuery('#reflection').width(jQuery('span').width());
        }).on('input', function () {
            jQuery('#vanity-span').text(jQuery('#reflection').val());
            jQuery('#reflection').width(jQuery('span').width());
        });
        jQuery('#vanity-url').on('input', function() {
            jQuery('#reflection').val(jQuery(this).val());
        });
    });
    body {
      background-color: #e4e4e4;
      font-family: Arial;
    }
    
    #vanity-url-wrapper {
      margin-top: 3em;
      text-align: center;
    }
    
    #vanity-url-wrapper > span {
      background-color: #FBE3CF;
      border-radius: 8px;
      padding: 0.5em 0;
      border: 2px solid orange;
    }
    
    .pre-span {
      background-color: orange;
      color: white;
      font-weight: bold;
      padding: 0.5em;
    }
    
    #vanity-url {
      display: block;
      text-align: center;
      width: 12em;
      margin: 3em auto;
      font-size: 1.2em;
      border-radius: 5px;
      border: 2px solid orange;
      padding: 0.5em;
    }
    
    #vanity-span{
      padding: 0.5em;
    }
    
    #reflection {
      display: none;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <body>
      <div id="vanity-url-wrapper">
        <span>
          <span class="pre-span">https://example.com/</span>   
          <span id="vanity-span"></span>
          <input id="reflection" type="text" readonly>
          <span class="pre-span">/</span>
        </span>
      </div>
      <input id="vanity-url" type="text" placeholder="Type here your vanity URL">
    </body>