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

82
Views
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 answers
Answer question

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