title: get the new added characters to an input by js
body: <p>I know this seems a quite easy target.  I have an <code>input[type=text]</code>, and I want to detect the new added character(s) in it.  The normal way is:</p>
  
  <pre><code>$selector.keypress(function(e) {
      //do sth here
      var newchar = String.fromCharCode(e.which);
  });
  </code></pre>
  
  <p>But the above method not working properly for some <strong>browsers</strong> on android devices.  Typing the android virtual keyboard will not fire the <code>keypress</code>.  </p>
  
  <p>Then I found the following method is better:</p>
  
  <pre><code>$selector.on('input', function(e){
      //do sth here
  });
  </code></pre>
  
  <p>It works fine for android devices, and also, it can detect <code>cut/paste</code>.  </p>
  
  <p>Now the question is, is there a way to know the new added character(s) to the <code>input</code>?  Do I need to do the complicated string comparison during inputing each time, i.e. compare the previous string and the new string in the input box?  I said it's complicated because you may not always type in char(s) at the end, you may insert some char(s) in the middle of the previous string.  Think about this, the previous string in the input box is <strong>"abc"</strong>, the new string after pasting is <strong>"abcxabc"</strong>, how can we know the new pasted string is <strong>"abcx"</strong>, or <strong>"xabc"</strong>?</p>
  
  <p>The method from keypress is quite simple:</p>
  
  <pre><code>String.fromCharCode(e.which);
  </code></pre>
  
  <p>So, is there similar way to do this by the <code>on('input')</code> method?</p>
  
  <hr>
  
  <p>After reading Yeldar Kurmangaliyev's answer, I dived into this issue for a while, and find this is really more complicated than my previous expectation.  The key point here is that there's a way to get the cursor position by calling: <code>selectionEnd</code>.</p>
  
  <p>As Yeldar Kurmangaliyev mentioned, his answer can't cover the situation: </p>
  
  <blockquote>
    <p>it is not working is when you select text and paste another text with
    replacing the original one.</p>
  </blockquote>
  
  <p>Based on his answer, I modified the <code>getInputedString</code> function as following:</p>
  
  <pre><code>    function getInputedString(prev, curr, selEnd) {
          if (selEnd === 0) {
            return "";
          }
          //note: substr(start,length) and substring(start,end) are different
          var preLen = prev.length;
          var curLen = curr.length;
          var index = (preLen &gt; selEnd) ? selEnd : preLen;
          var subStrPrev;
          var subStrCurr;
          for(i=index; i &gt; 0; i--){
              subStrPrev = prev.substr(0, i);
              subStrCurr = curr.substr(0, i);
              if (subStrCurr === subStrPrev) {
                  var subInterval = selEnd - i;
                  var interval = curLen - preLen;
                  if (interval&gt;subInterval) {
                      return curr.substring(i, selEnd+(interval-subInterval));
                  }
                  else{
                      return curr.substring(i, selEnd);
                  }
  
              }
          }
  
          return curr.substring(0, selEnd);
      }
  </code></pre>
  
  <p>The code is quite self explanation.  The core idea is, no matter what character(s) were added(type or paste), the new content MUST be ended at the cursor position.</p>
  
  <p>There's also one issue for my code, e.g. when the prev is <code>abcabc|</code>, you select them all, and paste <code>abc</code>, the return value from my code will be <code>""</code>.  Actually, I think it's reasonable, because for my scenario, I think this is just the same with delete the <code>abc</code> from previous <code>abcabc|</code>.</p>
  
  <p>Also, I changed the <code>on('input')</code> event to <code>on('keyup')</code>, the reason is, for some android browsers, the <code>this.selectionEnd</code> will not work in a same way, e.g., the previous text is <code>abc|</code>, now I paste <code>de</code> and the current string will be <code>abcde|</code>, but depending on different browsers, the <code>this.selectionEnd</code> inside <code>on('input')</code> may be <strong>3</strong>, or <strong>5</strong>.  i.e. some browsers will report the cursor position before adding the input, some will report the cursor position after adding the input.  </p>
  
  <p>Eventually, I found <code>on('keyup')</code> worked in the same way for all the browsers I tested.</p>
  
  <p>The whole demo is as following:</p>
  
  <p><a href="http://jsfiddle.net/hrhktkbzyy/ascfLfo6/4/" rel="nofollow">DEMO ON JSFIDDLE</a></p>
  
  <p>Working on the cross-browser compatibility is always difficult, especially when you need to consider the touch screen ones.  Hope this can help someone, and have fun. </p>