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

127
Vistas
I want to fetch and insert an attribute multiple times

I want to fire the contents of all attributes in a custom element <my-component> . Currently, only the preceding attribute is executed.

If it works, this will be <p>hello, world! welcom </p> .

Also, I want the {{ }} part to be executed with or without spaces.

function component(elementName, ComponentOptions) {
  customElements.define(`${elementName}`, class extends HTMLElement {
    connectedCallback() {
      if (ComponentOptions.return) {
        if (this.getAttributeNames()) {
          const AttrNames = this.getAttributeNames();
          var optionsreturn = ComponentOptions.return;
          AttrNames.forEach(attr => {
            let val = this.getAttribute(attr);
            optionsreturn = optionsreturn.replace(`{{ ${attr} }}`, val);
            this.outerHTML = optionsreturn;
          });
        } else {
          this.outerHTML = ComponentOptions.return
        }
      }
    }
  });
}

component("my-component", {
  return: `<p>hello, {{ w }} {{ wel }} </p>`
})
<my-component w="world!" wel="welcom"></my-component>

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

0

You need to move your this.outerHTML = optionsreturn; outside the forEach loop.

I also changed the string into a regular expression in your .replace() call. It will now accept zero or one spaces surrounding your attribute names.

function component(elementName, ComponentOptions) {
  customElements.define(elementName, class extends HTMLElement {
    connectedCallback() {
      if (ComponentOptions.return) {
        if (this.getAttributeNames()) {
          const AttrNames = this.getAttributeNames();
          var optionsreturn = ComponentOptions.return;
          AttrNames.forEach(attr => {
            let val = this.getAttribute(attr);
            optionsreturn = optionsreturn.replace(new RegExp(`\{\{ ?${attr} ?\}\}`,"g"), val);
          });
          this.outerHTML = optionsreturn;
        } else {
          this.outerHTML = ComponentOptions.return
        }
      }
    }
  });
}

component("my-component", {
  return: `<p>hello, {{ w }} {{wel}} </p>`
})
<my-component w="world!" wel="welcom"></my-component>

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