Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

212
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda