Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

173
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!