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

80
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
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