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

158
Vistas
Custom HTML Form Element with Shadow DOM gives blank html page

The code is from this page https://usefulangle.com/post/362/custom-elements It builds a custom html form element with shadow DOM.

But, when I open it in a browser, it's blank. enter image description here

I opened the code in "Inspect" and saw this error.

enter image description here

class InputPlusMinus extends HTMLElement {
  constructor() {
    super();

    let template = document.querySelector('#input-plus-minus-template').content;
    this.attachShadow({
      mode: 'open'
    }).appendChild(template.cloneNode(true));

    let add_button = this.shadowRoot.querySelector("#add");
    let subtract_button = this.shadowRoot.querySelector("#subtract");
    let count_textbox = this.shadowRoot.querySelector("#count");

    this.setAttribute('value', '1');

    add_button.addEventListener('click', () => {
      let current = parseInt(count_textbox.value, 10);
      count_textbox.value = current + 1;
      this.setAttribute('value', count_textbox.value);
    });

    subtract_button.addEventListener('click', () => {
      let current = parseInt(count_textbox.value, 10);
      if (current > 1) {
        count_textbox.value = current - 1;
        this.setAttribute('value', count_textbox.value);
      }
    });
  }
}

customElements.define('input-plus-minus', InputPlusMinus);
<template id="input-plus-minus-template">
        <style>
        button {
            width: 25%;
            /* other CSS */
        }
    
        input {
            width: 50%;
            /* other CSS */
        }
        </style>
        <div id="container">
            <button id="subtract">-</button>
            <input type="text" id="count" value="1" readonly />
            <button id="add">+</button>
        </div>
    </template>


<!-- custom element being used -->
<input-plus-minus id="sample"></input-plus-minus>

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

0

Based on the comments, I modified the code and it works now. Thank You so much!!

class InputPlusMinus extends HTMLElement {

  connectedCallback() {
    let template = document.querySelector('#input-plus-minus-template').content;
    this.attachShadow({
      mode: 'open'
    }).appendChild(template.cloneNode(true));

    let add_button = this.shadowRoot.querySelector("#add");
    let subtract_button = this.shadowRoot.querySelector("#subtract");
    let count_textbox = this.shadowRoot.querySelector("#count");

    this.setAttribute('value', '1');

    add_button.addEventListener('click', () => {
      let current = parseInt(count_textbox.value, 10);
      count_textbox.value = current + 1;
      this.setAttribute('value', count_textbox.value);
    });

    subtract_button.addEventListener('click', () => {
      let current = parseInt(count_textbox.value, 10);
      if (current > 1) {
        count_textbox.value = current - 1;
        this.setAttribute('value', count_textbox.value);
      }
    });
  }
}

customElements.define('input-plus-minus', InputPlusMinus);
<template id="input-plus-minus-template">
            <style>
            button {
                width: 25%;
                /* other CSS */
            }
        
            input {
                width: 50%;
                /* other CSS */
            }
            </style>
            <div id="container">
                <button id="subtract">-</button>
                <input type="text" id="count" value="1" readonly />
                <button id="add">+</button>
            </div>
        </template>


<!-- custom element being used -->
<input-plus-minus id="sample"></input-plus-minus>

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