Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

213
Vistas
Vanilla Javascript component not working inside another component

I made a search bar component in javascrip

const template = document.createElement("template");
template.innerHTML = `
<div class="search">
        <div className="searchIcon">
            <img src="searchIcon.png" alt="search" width="13rem" />
        </div>
        <input type="text" placeholder="Search" />
    </div>
`;

class SearchBar extends HTMLElement {
  constructor() {
    super();
    const el = this.attachShadow({ mode: "open" });
    el.appendChild(template.content.cloneNode(true));
    this.search = el.querySelector("input");
    this.search.addEventListener("focus", () => {
      this.search.style.outline = "none";
    });
    
  }
}

customElements.define("search-bar", SearchBar);

and after that I created another component where I tried to insert my custom search bar:

const template = document.createElement("template");
template.innerHTML = `
      <div class="navContainer">
          <div class="titleContainer itemFlex">
            <img src="arrow.png" alt="sageata" width="15rem"                          height="15rem" />
            <h2 class="title">TITLURILE.RO</h2>
          </div>
          <div class="itemFlex searchContainer">
              <search-bar></search-bar>
          </div>
       </div>`;


class TopBar extends HTMLElement {
  constructor() {
    super();
    const element = this.attachShadow({
      mode: "open",
    });
    element.appendChild(template.content.cloneNode(true));
  }
}

customElements.define("top-bar", TopBar);

My question is how do I make the two js files to communicate with each other in order to display my search-bar component into the top-bar.

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda