Search code examples
jquerykeyup

Finding and adding value to closest input field on keyup


I want to add a value to closest input field on keyup. But it is not working in dynamically added fields.

$(document).on('keyup', '.rate_qar' , function() {
    var qar_rate = $('#conv_rate_usd').val();
    var usd_rate = 10*qar_rate;
    $(this).parent('tr').find('input.rate_usd').val(usd_rate);
});

But it is not working. How can i do that? My HTML is given below. Please note that html inputs will be added dynamically.

<input name="rate_qar[]" type="number" min="1" placeholder="Room rate QAR" class="form-control rate_qar">
<input name="rate_usd[]" type="number" min="1" placeholder="Room type USD" class="form-control rate_usd"  >

Solution

  • The problem could be tr won't be a parent of input, so use .closest() instead of .parent()

    $(this).closest('tr').find('input.rate_usd').val(usd_rate);