6
Whenever I want to test something through the web I end up using the console browser
(F12 > Console) to check some behaving, especially in the case of javascript. However, I recently noticed that the event .phocus() does not work as expected.
See the example below:
<input id="teste" type="text" id="teste" />
<br/><br/><br/>
<button onclick="document.getElementById('teste').focus()">teste</button>
If I click the button "test", the input
will have focus, but if I type the same command that is in the onclick()
in the console
, nothing happens.
Faced with this, how to do the .focus()
function and why this occurs?
Note: This is not just because of the snippet. If I try to do the same with the
input
Sopt’s research, nothing happens:
document.querySelector('.top-bar .searchbar input[type="text"].f-input').focus();
I know it’s not in the question, but is there a specific reason for
.click()
function and the.focus()
no? For example, on the Sopt website we can use this to go to the profile:document.querySelector('.top-bar .my-profile').click();
– Randrade
@Randrade I find fully valid for the subject, since it refers to the policies of behavior, note that
.click
works in most cases, others will not (like opening certain types of windows), for other common tasks the click is something that can be programmable because the working policy probably understands that it will not affect the user directly, that is to be an action inside the page and will not control the "mouse/mouse", since Focus would probably control the pointer, and what was typed would enter the field. I can’t say for certain, but I promise to bring sources ;)– Guilherme Nascimento