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

176
Vistas
I'm trying to hang 2 functions on one click, and getting an error, what am I doing wrong?

So, basically, I'm trying to replace the value for the span, but it throws an error after click

const addListenerToEditButtons = (arr) => {
        const updatedButtons = document.querySelectorAll('.container-buttons__update');
        updatedButtons.forEach((button, index) => {
            button.addEventListener('click', (e) => {
                const targetSpan = e.target.parentElement.parentElement.parentElement.children[3].children[0];
                const targetInput = e.target.parentElement.parentElement.parentElement.children[3].children[1];
                toggleEditMode(targetSpan, targetInput);
                editNote(arr, index, targetSpan, targetInput);
            })
        })
    }
    
    const toggleEditMode = (span, input) => {
        if (input.classList.contains(HIDE_ELEMS)) {
            span.classList.add(HIDE_ELEMS);
            input.classList.remove(HIDE_ELEMS);
            return
        }
        span.classList.remove(HIDE_ELEMS);
        input.classList.add(HIDE_ELEMS);
    }
    
    const editNote = (arr, index, span, input) => {
    
        arr.filter((item, i) => {
            if (i === index && input.value !== '') {
                item.content = input.value;
                return
            }
            toggleEditMode();
        })
        showAllNotes(arr);
    }

I'm getting an error: Uncaught TypeError: Cannot read properties of undefined (reading 'classList')

here is you can test my code: https://codesandbox.io/s/winter-silence-yjxo3?file=/package.json

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

0

classList is undefined because you didn't pass the span & input element in the toggleEditMode function. This would solve your error but your application won't work still because you don't wait for the user's input before you execute the toggleEditMode function again. So the user won't even have the time to fill something in.

Waiting for user input is a against the principles of JavaScript in my opinion. A better solution would be to attach an input listener to all input element like this:

const addListenerToEditButtons = (arr) => {
  const updatedButtons = document.querySelectorAll(
    ".container-buttons__update"
  );

  updatedButtons.forEach((button, index) => {
    const targetSpan =
        button.parentElement.parentElement.children[3]
            .children[0];
    const targetInput = button.parentElement.parentElement.children[3]
        .children[1];

    targetInput.addEventListener("input", (evt) => {
      targetSpan.innerText = evt.target.value; // here you set the span to the value of the input. You would probably want to store it somewhere here.
    });

    button.addEventListener("click", (e) => {
      toggleEditMode(targetSpan, targetInput); // here you make the input field visible when the edit button is clicked.
    });
  });

Then in your toggleEditMode function you can also put the existing text in the input field and maybe autoFocus on it. If you only want to save the value when the user clicks the edit button again this would be the place to do it:

const toggleEditMode = (span, input) => {
  if (input.classList.contains(HIDE_ELEMS)) {
    span.classList.add(HIDE_ELEMS);
    input.classList.remove(HIDE_ELEMS);
    input.value = span.innerText;
    input.focus(); // so that the input field can automatically be typed into.
    return;
  }
  span.classList.remove(HIDE_ELEMS);
  input.classList.add(HIDE_ELEMS);
};
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