It works only with .focus();
Any idea?
Thanks!
Hi there!
The problem is that you are using jQuery selectors instead of native JavaScript selectors. To make your code working properly, replace it with:
document.querySelector('span').addEventListener('click', function () {
document.getElementById('InputToFocus').focus({
preventScroll: true
});
});
Thanks. By the way this means this cant work with jquery?
There is a difference between jQuery and JavaScript.
jQuery: the function “focus” can take “handler” (which is a function) as an argument. Here’s the link https://api.jquery.com/focus/
Native JavaScript: the function “focus” can take “focusOption” which is an Object parameter. Here’s the link https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/focus
I think the support for preventScroll is also questionable Browser compatibility (Tested it in Firefox and it does not seem to work.)