D
D
DAnya782021-10-17 00:47:18
Python
DAnya78, 2021-10-17 00:47:18

How to enter text into input?

There is a search engine on YouTube and you need to enter text there, but it doesn’t work with a script, but I understand that it works on pressing and then you can enter this, I tried it didn’t work
document.getElementById('search').value = 'Hi'

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nadim Zakirov, 2021-10-17
@zkrvndm

This is not enough, it is necessary for the JavaScript on the page to understand that the text in the field has changed.
After inserting, make the handlers that the site set for this field work:

document.querySelector('#search').dispatchEvent(new Event('input'));
document.querySelector('#search').dispatchEvent(new Event('change'));

More about browser events:
https://learn.javascript.ru/dispatch-events#method-...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question