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

204
Views
How to get node elements for the elements requested via fetch API?

I am making a single page application which all pages are requested via fetch api. The problem is that, after successfully fetching contents, i cannot be able to access the using document.querySelector() or document.querySelectorAll() and when i try to check if div with class mainCont is empty or not as shown on codes below, it returs empty child nodes while the content from fetch (data) are displayed to the users window but not on document source code.

const mainCont = document.querySelector('.mainCont');
const pathnamesList = ['', 'index', 'logout', 'profile', 'security', 'setting'];
const wP = window.location.pathname;
const pathnameSplit = wP.split('/', 3)[1];

/* -- fetch request -- */
async function loadViews(url) {
    const response = await fetch(url)
    .then(response => response.text())
    .then(data => {
        mainCont.innerHTML = data;
    }).catch((error) => console.log(error));
    return;
}
/* -- // -- */

if (pathnamesList.indexOf(pathnameSplit) !== -1 && wP !== '/') {
    document.title = `demoDomain | ${pathnameSplit}`;
    loadViews(`${pathnameSplit}.html`);
    if (mainCont.childElementCount == 0) {
        console.log('Empty div');
    }else {
        console.log(mainCont.childNodes);
    } 
    console.info(pathnameSplit);
} else {
    console.error('No such directory');
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="main.css">
    <link rel="stylesheet" href="index.css">
</head>
<body>
    <div class="mainCont">
        
    </div>
    <script src="route.js"></script>
    <script src="main.js"></script>
</body>
</html>

Is there a possible way to access those links (DOM elements) from fetch API via vanilla javascript?Image of the elements that have successfully fetched but their source codes are not shown in pages source

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