0

I am getting error "cannot set property of 'onkeydown' of null" for the following code.

Please anybody can suggest why am I getting this error

document.getElementById('customerId').onkeydown = function(evt) {
  debugger
  evt = evt || window.event;
  var keyCode = evt.keyCode;
  var enterkey = (keyCode == 13);
  if (enterkey) {
    var customerCode = $('#customerId').val();
    $.ajax({
      type: 'POST',
      url: "/Home/addTaxNotice",
      data: JSON.stringify({
        value: customerCode
      }),
      contentType: "application/json; charset=utf-8",
      success: function(data) {}
    });
  }
}
<input class="form-control" id="customerId" maxlength="6" />
mplungjan
  • 134,906
  • 25
  • 152
  • 209
Ben
  • 5
  • 2
  • 2
    it seems that the element selector did not find the tag with a given id, could you tell us where did you place the script tag? it may be order issue, where script is triggered before the tag is added to the body – Krzysztof Krzeszewski Apr 20 '21 at 14:00
  • Since the snippet I made you does not give the error, I expect the dupe gives you the answer – mplungjan Apr 20 '21 at 14:14
  • //input code goes here – Ben Apr 21 '21 at 07:59
  • I added an onclick function in the input tag and added all script code inside function and its working for me know. Thank you for reverting. – Ben Apr 21 '21 at 08:11

1 Answers1

0

The problem is here: document.getElementById('customerId').onkeydown, probably you entered either wrong id of your element, or your element does not exist, or hasn't been created yet.