javascript - how to get previous value of textbox in jquery on change event - Stack Overflow

$(".whitebox").on('change', function () {var previousAmt = $(this).data("old&qu

 $(".whitebox").on('change', function () {
       var previousAmt = $(this).data("old"); //  i want previous value
        var id = this.id;
        var slpitid = id.split("_");

        var checkboxid = "#chck_" + slpitid[0];
        if ($(checkboxid).is(':checked')) {
            var tipFlag = true;
        }
        else {
            var tipFlag = false;
        }

        if (tipFlag === true)
        {
            $("#amounttipchange").modal('show');
        }
        //alert(previousAmount);
    });

 $(".whitebox").on('change', function () {
       var previousAmt = $(this).data("old"); //  i want previous value
        var id = this.id;
        var slpitid = id.split("_");

        var checkboxid = "#chck_" + slpitid[0];
        if ($(checkboxid).is(':checked')) {
            var tipFlag = true;
        }
        else {
            var tipFlag = false;
        }

        if (tipFlag === true)
        {
            $("#amounttipchange").modal('show');
        }
        //alert(previousAmount);
    });

i want to store previous value on change event how can i perform in jquery i am not able to store value using $(this).data("old"); any help will be appreciated

Share Improve this question edited Jul 6, 2017 at 15:26 Inian 86.2k15 gold badges163 silver badges181 bronze badges asked Jul 6, 2017 at 14:32 user6885473user6885473 3281 gold badge5 silver badges25 bronze badges 3
  • Please add your HTML code too – Ionut Necula Commented Jul 6, 2017 at 14:33
  • i am not able to store value using $(this).data("old") -- why? – Steve Danner Commented Jul 6, 2017 at 14:34
  • previousAmt =$(this).data("old") output is emtpty – user6885473 Commented Jul 6, 2017 at 14:40
Add a ment  | 

3 Answers 3

Reset to default 3

You almost got this right, but you forgot to update the data-old in your code, below is an example and jsbin

 $(".whitebox").on('change', function (event) {
   // on first change will be empty string
   // as no previous value was set
   var oldValue = event.target.dataset.old;
   console.log('old value', oldValue);

   // your code here

   // at the end assign new value to data-old attribute
   $(this).data('old', event.target.value);
   console.log(event.target.value);
 });

http://jsbin./jasaqiwuwo/

Try this:

$("#checkboxid").on('click',function(){
   $(this).data('old',$(this).val());
});

And then use your change event.

var prevVal = $('input').val(); //init val
var valuesHistory = [prevVal]; 
$('input').data('prevData', prevVal);

$('input').on('change', function() {
  console.log('prev value from data: ', $(this).data('prevData'));
  console.log('prev value from var: ', prevVal);
  console.log('log: ', valuesHistory);
  valuesHistory.push($('input').val());
  $('input').data('prevData', $('input').val());
  prevVal = $(this).val();
});
// you can also get the prev value, like that valuesHistory[valuesHistory.length-2];
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text">

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745226936a4617504.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信