简体   繁体   中英

jQuery Get Value From Dynamically Created Content after .keypress() with .delegate()

I have this code which seems unable to retrieve the value of particular ID on .keypress function after using .delegate . The ID and other contents are actually generated dynamically via AJAX call.

    $(document).delegate('.edit_tr', 'click', function(){

        var cont_det_id = $(this).attr('id');
        $("#name_" + cont_det_id).hide(); //hide span
        $("#input_name_" + cont_det_id).show(); //show editable box
        //alert cont_det_id works good and gets correct ID

    }).keypress(function(e){
        if(e.which == 13) { //if statement for pressing enter button
            var cont_det_id = $(this).attr('id');
            //alert cont_det_id gets undefined
        }
    });

I am using old template that I was working on previously so the jQuery version is 1.7.x. Anybody has any idea for the workaround? Thanks.

Arun P Johny suggestion in the comment worked for me. So I post it here with hope that this could benefit somebody.

$(document).delegate('.edit_tr', 'click', function(){

    var cont_det_id = $(this).attr('id');
    $("#name_" + cont_det_id).hide(); //hide span
    $("#input_name_" + cont_det_id).show(); //show editable box
    //alert cont_det_id works good and gets correct ID

}).delegate('.edit_tr', 'keypress', function (e) {
    if(e.which == 13) { //if statement for pressing enter button
        var cont_det_id = $(this).attr('id');
        //alert cont_det_id good here
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM