Thursday 6 March 2014

How to retain old text box value using JQuery

In my scenario, i have one table that contains multiple rows, each row will have one checkbox. Once you checked the checkbox , other column(example invoice balance there is some numeric amount column) value has to populate in other text box value which is applied amount column. Once you uncheck the checkbox you should retain what was the old value in applied amount column, may be its came from db.
The following image will show the exact scenario what i am trying to say above.

How to retain old text box value using jquery










After lot of googling, i found a solution in single line of code using pure javascript. But Many people they are achieving same stuff using temp variable. Those are really not needed in my observation. Lets jump into exact solution how to fix it....

<script language="JavaScript">
function check_invoice_row_status(check_box)
{
var current_amount_old_value = $("#"+check_box.id+"").parent().parent().find("input[type='text'][id*='current_amount']").prop("defaultValue");
var adjustment_amount_old_value = $("#"+check_box.id+"").parent().parent().find("input[type='text'][id*='adjustment_amount']").prop("defaultValue");
      $("#"+check_box.id+"").parent().parent().find("input[type='text'][id*='current_amount']").val(current_amount_old_value);
      $("#"+check_box.id+"").parent().parent().find("input[type='text'][id*='adjustment_amount']").val(adjustment_amount_old_value);
}

</script>

Main code which i mention one line code is  $("input[type='text'][id*='current_amount']").prop("defaultValue") ; Will return the old value(db saved value).

Thanks for showing interest to read this article.