i have a text field having class=answer and have 12 keys 1,2,3,4,5,6,7,8,9,0,del,clear.
$('.answer').on('click', function() {
  activeFill = $(this);
});
var lastAddedText = '';
$('.one,.two,.three,.four,.five,.six,.seven,.eight,.nine,.zero,.extra1,.extra2').on 'click',
  function() {
    if (activeFill !== 'undefined') {
      var lastAddedText = $(this).html();
      // append content
      var prevContent = $(activeFill).val();
      // if condition to check if text already present or not
      $(activeFill).val(prevContent + lastAddedText);
    }
  });
$('.delete').on('click', function() {
  var $myInput = $(activeFill);
  $myInput.val($myInput.val().slice(0, -1));
});
$('button.clear').on('click', function() {
      $(active<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" class="answer" />
<div class="keys">
  <center>
    <button class="one" style="font-weight:bold">1</button>
    <button class="two" style="font-weight:bold">2</button>
    <button class="three" style="font-weight:bold">3</button>
    <button class="four" style="font-weight:bold">4</button>
    <button class="five" style="font-weight:bold">5</button>
    <button class="six" style="font-weight:bold">6</button>
    <button class="seven" style="font-weight:bold">7</button>
    <button class="eight" style="font-weight:bold">8</button>
    <button class="nine" style="font-weight:bold">9</button>
    <button class="zero" style="font-weight:bold">0</button>
    <button class="extra1" style="font-weight:bold">i</button>
    <button class="extra2" style="font-weight:bold">-</button>
    <button class="clear" style="font-weight:bold">Clear</button>
    <button class="delete" style="font-weight:bold">Del</button>when i am clicking the textfield and then the key the value of the key comes to the textfield but the problem is suppose i have entered 124 in textfeild and now i want to enter 3 before 4 so i click my cursor before 3 and press the key but still 3 is typed after 4 like 1243.
$('.one,.two,.three,.four,.five,.six,.seven,.eight,.nine,.zero,.extra1,.extra2,.extra3,.extra4,.extra5,.extra6,.extra7').on('click',function() {
var prev = $("#txt1").val();
var num = this.value;
var pos = $("#txt1")[0].selectionStart;
var newValue = prev.substring(0, pos) + num + prev.substring(pos);
$("#txt1").val(newValue);
$("#txt1")[0].setSelectionRange(pos+1, pos+1);
var prev1 = $("#txt").val();
var num1 = this.value;
var pos1 = $("#txt")[0].selectionStart;
var newValue1 = prev.substring(0, pos1) + num + prev.substring(pos1);
$("#txt").val(newValue1);
$("#txt")[0].setSelectionRange(pos1+1, pos1+1);
      });
html
    <div class="cnt3"><input type='text' id="txt" class='answer1'  style='background-      color:#FFFFA8; height:32px;font-size:24px; width:400px; text-align:center; color:red;' />
 <div class="cnt5"><input type='text' id="txt1" class='answer2'  style='background-color:#FFFFA8; height:32px;font-size:24px; width:400px; text-align:center; color:red;' /></div>
 
     
     
    