1

I have an input field in each row of my jquery datatable. I have to trigger an event on text change and enter press for each of these input fields.Am loading jquery datatable using serverside processing.Before without using the server side processing the input field events were working fine!.What causes the event to be silent now?

Events i used before ---

$('#txtQty').keydown(function (e) {
        alert("keydown");
}

$('#txtQty').change(function () {
         alert("Change");

}

same listeners i use after the serverside processing applied.

Jquery grid details- client side processing and adding input box~

@foreach (var item in Model)
{
 <tr>
                    .
.
.
.
.
.
@if (item.Qty <= 0)
{
 <td>
     <input class="inputs" id="txtQty" type="text" [email protected] />
     </td>
    }
  }

Serverside processing and input box applied on the go-

$('#grid').dataTable({
        "bServerSide": true,
        "sAjaxSource": "../myaction/AjaxHandler",
        "bProcessing": true,
        "scrollY": 385,
        "scrollX": true,
        "scrollCollapse": true,
        "jQueryUI": true,
        "bJQueryUI": true,
        "sDom": 'lfrtip',

        "aoColumns": [
                        { "sName": "dfgdfg" },
                        { "sName": "dfgdfg" },
                        { "sName": "hhh" },
                        {
                            "sName": "Qty",

                            "mRender": function (sName) {

                                return '<input class="inputs" id="txtQty" type="text"  value='+ sName +' />';
                            },
                        },
                        { "sName": "Category" },
                        { "sName": "Comment" }
        ],
        "oLanguage": {

            "sProcessing":'Processing.....'
        }

    });
4
  • You have multiple elements with same ID txtQty, don't you? If so, replace $('#txtQty').keydown with, for example, $('.inputs').keydown Commented Sep 4, 2014 at 10:03
  • @Regent tried it didnt work!! Commented Sep 4, 2014 at 10:17
  • 1
    Due to dynamically created elements use $(document).on('keydown', '.inputs', function(e) instead. Commented Sep 4, 2014 at 10:22
  • @Regent please add above comment as answer let me say that's the correct answer. Commented Sep 4, 2014 at 10:27

2 Answers 2

1
  • you have multiple elements with same id txtQty. For correct event handling of multiple elements you can use class. For example, class inputs.

  • for dynamically created elements you can use $(document).on("event", "selector", function() {});.

So, finally, it should look like:

$(document).on('keydown', '.inputs', function(e) {
    alert("keydown");
}

$(document).on('change', '.inputs', function() {
    alert("Change");
}
Sign up to request clarification or add additional context in comments.

Comments

1

I suppose there are more than one "txtQty" per page so instead of an id, use a class

 <input class="inputs txtQty" type="text" [email protected] />


$('.txtQty').keydown(function (e) {
    alert("keydown");
}

$('.txtQty').change(function () {
     alert("Change");
}

2 Comments

no id required? why two classes? class="inputs txtQty"
this works on client side processing only for datatble in this case!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.