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

295
Vistas
How can I display in my HTML file what is returned in my external JS script?

I am working on a project that collects a piece of information from a website and returns that value in an HTML Document.

Here is what my HTML file looks like:

script type="text/javascript" src="website_fetch.mjs"></script>
            
    <section>
        <h2>Info on Google here</h2>
        <ul> 
            <article is= "site-status" name="Google" info = NEED RETURN VALUE FROM SCRIPT ABOVE ></article>

How can I get the returned value from this script? In the JS file, there is a return statement that I need to set equal to info in the <article> tag.

Any help greatly appreciated.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Alter the script so that it selects the article tag you're interested in and populates the attribute. For example, if you have

// website_fetch.mjs
export const getGoogleInfo = async () => {
  // implementation goes here
  return someInfo;
};

you can change it to

// website_fetch.mjs
export const getGoogleInfo = async () => {
  // implementation goes here
  return someInfo;
};
getGoogleInfo()
  .then((result) => {
    document.querySelector('article[is="site-status"]').setAttribute('info', result);
  });
  // .catch(handleErrors); // don't forget this part - don't ignore errors

Your getGoogleInfo is asynchronous, so you have to wait for its Promise to resolve to a value before inserting the info.

But there's another problem. Your HTML is invalid. An <article> cannot be a child of a <ul>. An unordered list can only have children that are list items (<li>). Fix your HTML so that it's valid, so that none of your markup gets dropped or parsed incorrectly.

Live demo:

// this is just to show that the article's info attribute is as desired:
setTimeout(() => {
  console.log(document.querySelector('section').innerHTML);
}, 1000);
<script type="module">
export const getGoogleInfo = async () => {
  // implementation goes here
  return 'someInfo';
};
getGoogleInfo()
  .then((result) => {
    document.querySelector('article[is="site-status"]').setAttribute('info', result);
  });
</script>

<section>
  <h2>Info on Google here</h2>
  <article is="site-status" name="Google" info=N EED RETURN VALUE FROM SCRIPT ABOVE></article>
</section>

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