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

83
Visualizações
Javascript fetch to filter by ID of fetched content

I am using JavaScript to fetch a document via ajax, but what I would like to do is filter the retrieved content by ID, in this example "#container".

In jQuery it can be done like this:

lightbox.load( "test.html #container" );

My JavaScript looks like this:

let response = fetch("test.html").then(function(response) {
    if (response.ok) {
        response.text().then(function(text) {
            lightbox.innerHTML = text;
        });
    }
});

The above is putting the entire text response into the lightbox object. I am not sure how to filter the response by the #container ID.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can create a new HTML element from the receiving HTML and then search for your container.

let response = fetch("test.html").then(function(response) {
    if (response.ok) {
        response.text().then(function(text) {
            const template = document.createElement('template');
            template.innerHTML = text;
            const lightBoxContent = template.content.querySelector('#container');
            lightbox.innerHTML = lightBoxContent.innerHTML;
        });
    }
});

Inspiration by: Creating a new DOM element from an HTML string using built-in DOM methods or Prototype

about 4 years ago · Juan Pablo Isaza Relatório
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