0

My program has a dynamic table which structure is:

<table id="selectedItems">
</table>

and i will append it using jquery :

var i=0;
    $("#selectedItems").html('');
    $("#selectedItems").append(
            '<tr>' +
            '<th width="30px"><?=loadLanguage("admindisplay",$this->session->userdata("jw_language"),"form-delete")?></th>'+
            '<th width="115px"><?=loadLanguage("admindisplay",$this->session->userdata("jw_language"),"invoice-qty")?></th>'+
            '<th><?=loadLanguage("admindisplay",$this->session->userdata("jw_language"),"invoice-item")?></th>'+
            '<th><?=loadLanguage("admindisplay",$this->session->userdata("jw_language"),"invoice-price")?></th>'+
            '<th><?=loadLanguage("admindisplay",$this->session->userdata("jw_language"),"invoice-discount")?></th>'+
            '<th><?=loadLanguage("admindisplay",$this->session->userdata("jw_language"),"invoice-subtotal")?></th>'+
            '</tr>'+
            '<tr>'+
                '<td></td>'+
                '<td class="qty"><input type="text" name="qtyPriceEffect'+i+'" class="required number qty" id="qtyItem'+i+'" value="0"/></td>'+
                '<td></td>'+
                '<td><input type="text" name="prcPriceEffect'+i+'" class="required currency qty" id="prcItem'+i+'" value="0"/></td>'+
                '<td><input type="text" name="dscPriceEffect'+i+'" class="required number qty" id="dscItem'+i+'" value="0"/></td>'+
                '<td id="subTotal'+i+'">0</td>'+
            '</tr>'
);

and i have a function :

$("input[type='text'][name*='PriceEffect']").focusin(function(){
    alert("a");
});

but it doesn't work for my appended input text. if i provide the input text from the start (not dynamic), it works. someone please help me.

go eng chun
  • 73
  • 2
  • 10

1 Answers1

0

You need to use event delegation for attaching events to dynamically added elements:

$("#selectedItems").on("focusin","input[type='text'][name*='PriceEffect']",function(){
    alert("a");
});
Felix
  • 37,892
  • 8
  • 43
  • 55