0

I have a query regarding sending an enter key stroke to an html element from chrome console.

First of all I have searched this forum already and found link

As explained, I have tried but my code is failing to click return key, hence this post.

My HTML:

<input type="text" class="arrow" onblur="inserttext('Enter company name or symbol',this);" onfocus="cleartext('Enter company name or symbol',this);" value="Enter company name or symbol" autocomplete="off" name="companyED" onkeydown="if(event.keyCode==13) return false;" onkeyup="onKeyUp(event,&quot;keyword&quot;);" id="keyword">

What I have tried:

async function search(){
  for(const val of x) {
     let ele = document.getElementById('keyword');
     ele.value = val;
     console.log("Clicking: " + val);
     ele.addEventListener("keyup", function(event) {
          event.preventDefault();
          if(event.keyCode === 13) {
              console.log("in if");
              ele.click();
          }
     });
  };
}

Expected: Enter button should get hit

Actual: ReferenceError is throwing. Enter key is not working

Ofcourse I am calling my function as 'search()' in new line

FayazMd
  • 386
  • 2
  • 22
  • `onkeyup="onKeyUp(event,"keyword");"` there seems to be a typo, didn't you mean `"` when writing `"`? – Nino Filiu Mar 17 '19 at 20:17
  • Actually, that isn't a typo. Since the attribute is surrounded by ", you have to encode any double quotes you want to use inside it. It will work this way. You could also rewrite it to use single quotes instead. – Christoph Herold Mar 17 '19 at 20:35
  • I'm not sure, if your `onkeydown="if(event.keyCode==13) return false;"` is actually preventing your `onkeyup` from firing, since the keydown is already getting cancelled. – Christoph Herold Mar 17 '19 at 20:37

0 Answers0