DON
DON

Reputation: 835

Key press event of a dynamically generated textbox

tr.append($("<td/>").html('<input type="text" name="unit_price" id="unit_price"/>'));

This is my dynamically generated text box i have tried many solutions like below but its not working help me geeks

$("#unit_price").live("keyup", function(){  
     //Your Code to handle the click.
    }); 

$(staticAncestors).on(eventName, dynamicChild, function() {});

Upvotes: 4

Views: 3276

Answers (3)

Deenadhayalan Manoharan
Deenadhayalan Manoharan

Reputation: 5444

Try this..

<table >
    <tr>
        <td class="test"></td>  

    </tr>
</table>

$(document).ready(function(){

$(".test").append($("<td/>").html('<input type="text" name="unit_price" id="unit_price"/>'));
     $("#unit_price").keyup(function(){
alert($(this).val());
  });
});

Demo:http://jsfiddle.net/cz1dvzvf/1/

Upvotes: 1

I&#39;m Geeker
I&#39;m Geeker

Reputation: 4637

Use this

$(document).on("keyup","#unit_price", function(){  
 //Results here
}); 

Upvotes: 0

Milind Anantwar
Milind Anantwar

Reputation: 82251

You need to use event delegation.

Event delegation allows us to attach a single event listener, to a parent element, that will fire for all descendants matching a selector, whether those descendants exist now or are added in the future.

$("body").on("keyup","#unit_price", function(){  
 //Your Code to handle the click.
}); 

Upvotes: 2

Related Questions