0

How do I programmatically click on an input box to make it become activated. I'm doing scripting through Developer Tools. Any help would be appreciated!

I have tried .focus() and .click() properties like:

      document.getElementById("myText").focus();

fiddle at http://jsfiddle.net/nep95vzj/ but it doesn't do anything.

user5938020
  • 63
  • 1
  • 3
  • 12

2 Answers2

0

If I understand what you're asking, you should be able to do it by dispatching an event:

var clickEvent = new MouseEvent("click", {
    "view": window,
    "bubbles": true,
    "cancelable": false
});

var inputBox = document.getElementById('inputBox');

inputBox.focus();
inputBox.dispatchEvent(clickEvent);

(I left the focus in there in case you also wanted the highlighting of the field that you'll get with focusing the element.)

MikeTheReader
  • 4,200
  • 3
  • 24
  • 38
  • Hey Mike. Thanks for answering. I tried this in console for the element provided in the fiddle. It is creating the variables correctly but doesn't really simulate the click. – user5938020 Aug 03 '19 at 04:07
0
document.getElementById("inputBox").dispatchEvent(new Event("focus"));
General Grievance
  • 4,555
  • 31
  • 31
  • 45
user5938020
  • 63
  • 1
  • 3
  • 12