Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

200
Views
Javascript: How do I focus the YT search bar?

I use a javascript to set the focus / cursor into the first input element. It works fine under almost all circumstances, but not on the following page:

https://www.youtube.com/

The textbox that I expected to get the focus is this one: enter image description here

It has the following selector (copied from Chrome):

#search

This is its Xpath:

//*[@id="search"]

Full XPath:

/html/body/ytd-app/div[1]/div/ytd-masthead/div[3]/div[2]/ytd-searchbox/form/div[1]/div[1]/input

And this is its code:

<input id="search" autocapitalize="none" autocomplete="off" autocorrect="off" name="search_query" tabindex="0" type="text" spellcheck="false" placeholder="Suchen" aria-label="Suchen" role="combobox" aria-haspopup="false" aria-autocomplete="list" dir="ltr" class="ytd-searchbox" style="outline: none;">

However, I didn't manage to select this search box.

What am I missing?

This is the javascript code:

function pSetFocus()
{
  var element = document.querySelector("*[autofocus]");
  if (element)
  {

  }
  else
  {
     element = document.querySelector("form input:not([type=hidden])");
  }

  if (element)
  {

  }
  else
  {
     element = document.querySelectorAll("#search.ytd-searchbox"); //Here is try to select the YT search bar...
  }

  if (element)
  {

  }
  else
  {
     return;
  }

  element.selectionStart = element.selectionEnd = element.value.length;
  element.focus();
  var y = element.getBoundingClientRect().top + window.scrollY;
  window.scroll(
  {
     top: y,
     behavior: "smooth"
  });
};

Thank you!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I figured out another (the most important) problem:

I feed my CEF3 based browser the function code itself, but I did not call it.

I am now using

Me.webkitx1.Eval(somecodeWithoutFunctionDeclaration)

It works.

Before, I used:

Me.webkitx1.Eval(somecodeWithFunctionDeclaration)

Here you see how I stripped the function header (which made it work for me):

  var element = document.querySelector("*[autofocus]");
  if (element)
  {

  }
  else
  {
     element = document.querySelector("form input:not([type=hidden])");
  }

  if (element)
  {

  }
  else
  {
     element = document.querySelector("#search"); 
  }

  if (element)
  {

  }
  else
  {
     return;
  }

  element.selectionStart = element.selectionEnd = element.value.length;
  element.focus();
  var y = element.getBoundingClientRect().top + window.scrollY;
  window.scroll(
  {
     top: y,
     behavior: "smooth"
  });
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!