1

I am trying to press enter key inside a textbox.

I am currently setting up value in the textbox using below code,

var ifrm = document.getElementById("IPage");
var txtValue = ifrm.contentWindow.document.getElementById("textboxid");
acc2.txtValue = "010";

Now, once this value is set inside textboxid , I want to press the enter key using JavaScript.

How can I achieve it?

Faran Saleem
  • 404
  • 1
  • 7
  • 31
  • Why do you want to press the enter key ? If you want to add a new line you can do that without simulating an enter key press. – Titus May 12 '19 at 08:53
  • I do not want to add a new line. basically it is a lookup field so after setting the value of textbox to 010 the enter will select the account starting from 010. so this is why i want to press enter key to automatically select the account – Faran Saleem May 12 '19 at 08:54
  • If I get this right you're trying to select a value from an autocomplete list. If that is the case, simulating an enter key press on the input element may not work. The answers to this question https://stackoverflow.com/questions/3276794/jquery-or-pure-js-simulate-enter-key-pressed-for-testing contains some methods for simulating an enter key press. – Titus May 12 '19 at 08:57
  • This is not working for me. – Faran Saleem May 12 '19 at 09:12

1 Answers1

0

Try this and see if it works for you.

<!DOCTYPE html>
<html>
<body>

<textarea id="myInput" value=""></textarea>

<script>
var input = document.getElementById("myInput");
input.addEventListener("keyup", function(event) {
  if (event.keyCode === 13) {
   var myValue = document.getElementById("myInput").value;
   event.preventDefault();
   alert(myValue); //For testing purposes - use this value to call your account number
  }
});
</script>
</body>
</html>

You might have to add some additional code to make sure it doesn't accept anything but an integer or whatnot, but I think the basic concept is there.

Try it here -- https://jsfiddle.net/ger0ab4x/3/

Christopher Bennett
  • 803
  • 1
  • 8
  • 20
  • Thanks for the reply but this is not what i want. You are alerting on pressing enter but i want to press enter using JS without even clicking the enter button just as we human click enter i want to click it that way using js – Faran Saleem May 12 '19 at 11:38
  • I'm not sure I understand what you mean. How do you press enter without pressing enter? Do you mean you don't want the alert? Replace it with a command to select the account. – Christopher Bennett May 12 '19 at 12:16
  • I mean I am filling the textbox using JavaScript as can be seen in my question and once the textbox is filled with text . I want to press enter programatically. – Faran Saleem May 12 '19 at 12:19
  • Oh, I See. That's a substantially more complicated... Is the length of the text the same every time? – Christopher Bennett May 12 '19 at 12:22
  • Couldn't you append the text as a string, and then check to see if anything has been appended? If it has, then it can "input" the text value. – Christopher Bennett May 12 '19 at 12:24
  • Yes for now the length is same – Faran Saleem May 12 '19 at 12:25
  • I do not have any issue with the text. All i need is to press enter programitically using JS – Faran Saleem May 12 '19 at 12:25
  • I didn't mean the text, I meant whatever value you're trying to input. Append it to the textbox, and have javascript check to see if anything has been appended. If it has, then instruct it to "submit " the value and then, have it do whatever you want to do with it (like display the account belonging to that value). – Christopher Bennett May 12 '19 at 12:55