1

Recently the following post has helped me simulate a keypress on click: Definitive way to trigger keypress events with jQuery

I found that the selector $("input") only seems to work if the page has an input that is visible and not disabled.

Is there a way to trigger a keypress on a hidden element or another element that will give the same result (properly trigger the key press event)?

Community
  • 1
  • 1
ScottyG
  • 3,204
  • 3
  • 32
  • 42
  • 1
    What is your use case? – xkcd149 Apr 11 '16 at 16:05
  • If you absolutely need a keypress event, you could just set `visibility` to `hidden` and `position` to `absolute` and move it off screen. This way the element will still be invisible, and not take up space as it normally would if you just used the `visibility` – Eihwaz Apr 11 '16 at 16:05
  • Ya, what could be the use case??? See [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – A. Wolff Apr 11 '16 at 16:08

1 Answers1

1

Disabled elements don't fire mouse events. Most browsers will propagate an event originating from the disabled element up the DOM tree, so event handlers could be placed on container elements. However, Firefox doesn't exhibit this behavior, it just does nothing at all when you click on a disabled element.

As showed here, I suggest you to create a trigger element that when clicked show/enable the input temporary, launch a focus/click event as the post you suggested and then hide it again.

Jonathan Argentiero
  • 5,687
  • 8
  • 29
  • 34