M. Alim M. Alim - 1 month ago 7
jQuery Question

How to set value in p tag using jq?

I am trying to search bank name onkeyup and want to put result in p tag but it's not happening. can help me anyone?

this is jq : //search bank name

$('input[name^="acc_voucher_bank[]"]').live('keyup',function(){
var acc_voucher_bank=$(this).val();
$(this).closest('tr').find('p.bankNameResult').show();
$.post("../accounting_model/cash_receive_daily_date_wise.php",{
acc_voucher_bank:acc_voucher_bank,
rand:Math.random()
},function(data){
$(this).closest('tr').find('p.bankNameResult').html(data);
})
})


html code :

<table width="100%" id="myTable">
<tr class="tr_view">
<td style="width:25%;">Bank Name</td>
<td style="width:30%;">Branch Name</td>
<td style="width:15%;">A/C. No</td>
<td style="width:15%;">Amount</td>
<td style="width:15%;">Action</td>
</tr>

<tr class="bg1" style="text-align:center;">
<td>
<input type="text" style="width:200px;" name="acc_voucher_bank[]" id="" placeholder="Bank Name" />
<p id="bankNameResult" class="bankNameResult" name="bankNameResult[]" style="position:absolute; margin:0px;"></p>
<input type="hidden" name="bank_id[]" id="" />
</td>

<td>
<input type="text" style="width:270px;" name="acc_voucher_branch[]" id="" placeholder="Branch Name" />
<p id="bankBranchNameResult" class="bankBranchNameResult" name="bankBranchNameResult[]" style="position:absolute; margin:0px;"></p>
<input type="hidden" name="bank_branch_id[]" id="" />
</td>

<td>
<input type="text" style="width:130px;" name="acc_bank_account_number[]" id="" placeholder="A/C No" />
<p id="bankAccountResult" class="bankAccountResult" name="bankAccountResult[]" style="position:absolute; margin:0px;"></p>
<input type="hidden" name="bank_account_no[]" id="" />
</td>

<td><input type="text" style="width:130px;" name='bank_amount[]' id="" placeholder="Amount" /></td>

<td><input type="button" value="+" class="addTr" /></td>
</tr>
</table>

Answer

That's because this inside the $.post callback function is a jQuery XHR object. You can assign the element which you want to change to another variable, so it won't get changed inside $.post callback:

$('input[name^="acc_voucher_bank[]"]').live('keyup',function(){     
    var acc_voucher_bank=$(this).val();
    $element = $(this).closest('tr').find('p.bankNameResult');
    $element.show();    
    $.post("../accounting_model/cash_receive_daily_date_wise.php",{   
        acc_voucher_bank:acc_voucher_bank,         
        rand:Math.random()
        },function(data){   
        $element.html(data);
    })
})
Comments