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

244
Views
addEventListener en JS solo funciona después de actualizar la página

El siguiente código debería funcionar de la siguiente manera: cada vez que presione el botón 'remove-btn' , debería eliminarse de localStorage. Actualmente, el código solo funciona si la página se actualiza antes de cada clic. ¿Como puedó resolver esté problema? ¡Gracias!

 function renderLinksList() { let linksList = ""; // creat an empty variable to manipulate the DOM outside for (let i = 0; i < myLinks.length; i++) { linksList += ` <li> <a href="${myLinks[i]}" target="_blank"> ${myLinks[i]} </a> <button class="remove-btn"> <svg class="close-icon" width="8px" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512"> <path d="M310.6 361.4c12.5 12.5 12.5 32.75 0 45.25C304.4 412.9 296.2 416 288 416s-16.38-3.125-22.62-9.375L160 301.3L54.63 406.6C48.38 412.9 40.19 416 32 416S15.63 412.9 9.375 406.6c-12.5-12.5-12.5-32.75 0-45.25l105.4-105.4L9.375 150.6c-12.5-12.5-12.5-32.75 0-45.25s32.75-12.5 45.25 0L160 210.8l105.4-105.4c12.5-12.5 32.75-12.5 45.25 0s12.5 32.75 0 45.25l-105.4 105.4L310.6 361.4z"/> </svg></button> </li > `; } listEl.innerHTML = linksList; } const removeBtn = document.getElementsByClassName("remove-btn"); for (let i = 0; i < myLinks.length; i++) { removeBtn[i].addEventListener("click", function () { myLinks.splice(i, 1); localStorage.setItem("myLinks", JSON.stringify(myLinks)); renderLinksList(); }); }

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Mueve el código que agrega los detectores de eventos a renderLinksList() , para que los nuevos botones que crees obtengan detectores de eventos.

 function renderLinksList() { let linksList = ""; // creat an empty variable to manipulate the DOM outside for (let i = 0; i < myLinks.length; i++) { linksList += ` <li> <a href="${myLinks[i]}" target="_blank"> ${myLinks[i]} </a> <button class="remove-btn"> <svg class="close-icon" width="8px" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512"> <path d="M310.6 361.4c12.5 12.5 12.5 32.75 0 45.25C304.4 412.9 296.2 416 288 416s-16.38-3.125-22.62-9.375L160 301.3L54.63 406.6C48.38 412.9 40.19 416 32 416S15.63 412.9 9.375 406.6c-12.5-12.5-12.5-32.75 0-45.25l105.4-105.4L9.375 150.6c-12.5-12.5-12.5-32.75 0-45.25s32.75-12.5 45.25 0L160 210.8l105.4-105.4c12.5-12.5 32.75-12.5 45.25 0s12.5 32.75 0 45.25l-105.4 105.4L310.6 361.4z"/> </svg></button> </li > `; } listEl.innerHTML = linksList; const removeBtn = document.getElementsByClassName("remove-btn"); for (let i = 0; i < myLinks.length; i++) { removeBtn[i].addEventListener("click", function() { myLinks.splice(i, 1); localStorage.setItem("myLinks", JSON.stringify(myLinks)); renderLinksList(); }); } }

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!