Rehan Khan Rehan Khan - 6 months ago 15
jQuery Question

How to assign a value to an EditorFor based on another editorFor value

What if after the document is ready and user enters an input in an editorfor and i want to read that input and assign it to another editorFor after user has finished inputting it?

For example I have an editorFor called

basicSalary
and another editorFor
grossSalary
. So initially when user has finished entering a value into
basicSalary
, that value should be copied to
grossSalary
. My working is:

$(document).ready(function () {
$("#basicSalary").on('input',function(){
$('#grossSalary').val($(this).val());
});


Which is ofcourse incorrect since it applies only when the document is ready i.e.,
$(document).ready(function ()
and user enters
basicSalary
afterwards.

Answer

user enters an input in an editorfor and i want to read that input and assign it to another editorFor after user has finished inputting it?

Use focusout event as shown:-

$("#basicSalary").on('focusout',function(){
   $('#grossSalary').val($(this).val());
});

Reference

DEMO