Skip to content
Advertisement

How call onkeyup javascript function from the table column Html.EditorFor

I am trying to call a function calculfac() from the column of a table for each changes using onkeyup event . The column is numeric column and if we type any value there , the all digits from that column should be stored in variable. But when I type the value in NetHrs column , the function is not called Here is the code

 <tbody>
       <tr>
           <td>@Html.EditorFor(model => model.FromDate, new { htmlAttributes = new { @class = "form-control datepicker w-100" } })</td>
       <td>@Html.EditorFor(model => model.ToDate, new { htmlAttributes = new { @class = "form-control datepicker w-100" } })</td>
         <td>@Html.EditorFor(model => model.NetHrs, new { onkeyup = "calculfac()",  htmlAttributes = new { type = "number", @class = "form-control  w-100 empHrs" } })</td>
          <td>@Html.EditorFor(model => model.HolidayEnt, new { htmlAttributes = new { @class = "form-control  w-100", @readonly = "readonly" } })</td>
        <td><a href="" title="Delete Rows">Delete</a></td>
        </tr>
   </tbody>
                                
<script>
function calculfac() {
        var nethrs = // Here the value from that column should be stored including typed value
    }
</script>

Advertisement

Answer

trying to call a function calculfac() from the column of a table for each changes using onkeyup event . The column is numeric column and if we type any value there , the all digits from that column should be stored in variable.

To achieve your requirement, you can refer to the following code sample.

<td>@Html.EditorFor(model => model.NetHrs, new { htmlAttributes = new { type = "number", @class = "form-control  w-100 empHrs", @onkeyup = "calculfac(this)" } })</td>

Or trigger that function via onchange event

<td>@Html.EditorFor(model => model.NetHrs, new { htmlAttributes = new { type = "number", @class = "form-control  w-100 empHrs", @onchange = "calculfac(this)" } })</td>

JS code

function calculfac(el) {
    var nethrs = $(el).val();
    console.log("new value is ", nethrs);

    //...
    //your code logic here
    //...
}
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement