Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

143
Visualizações
How to run a custom element's constructor after it has all of its required attributes?

I'm trying to create a custom element "color-box" that will need an attribute named "color" to identify the box's color. So for example, <color-box color="green"></color-box> will give a 100 pixels wide and 100 pixels tall square, with the background-color green. I don't have any problems when defining the element directly with HTML by doing <color-box color="red"></color-box>, everything goes fine, but in my case, I need to create this element with only javascript, like this:

let colorBoxElement = document.createElement("color-box");
colorBoxElement.setAttribute("color", "red");
document.body.appendChild(colorBoxElement)

What happens is that the constructor is getting ran before colorBoxElement.setAttribute("color", "red") and that's not what I want, because I'm using that attribute in my constructor. I want the constructor to run when all of it's required attributes are there (in this case, only 1: color) or when it's actually appended to the document body using document.body.appendChild(colorBoxElement)

Is there any possible way of doing this, or at least doing something similar to what I need? Here's my code:

// Defining the custom element
class ColorBox extends HTMLElement {
  constructor() {
    super()
    let shadow = this.attachShadow({mode: "open"})
    let container = document.createElement("div")
    container.style.width = "100px"
    container.style.height = "100px"
    switch(this.getAttribute("color")) {
      case "red":
        container.style.backgroundColor = "red"
        break;
      case "green":
        container.style.backgroundColor = "green"
        break;
      default:
        throw new Error("Color attribute not valid: " + this.getAttribute("color"))
    }
    shadow.appendChild(container)
  }
}
customElements.define("color-box", ColorBox)

// Creating the custom element
let colorBoxElement = document.createElement("color-box")
// This will get executed after the constructor was ran (problem)
colorBoxElement.setAttribute("color", "green")
// Appending the element to body
document.body.appendChild(colorBoxElement)
<body>
  <color-box color="red"></color-box>
</body>

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda