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

201
Views
cómo eliminar un detector de eventos cuando se agrega directamente en un archivo HTML

hay un botón que voy a agregar a la página con HTML interno, el botón tiene un detector de eventos onclick="addWatchlist()" y, a continuación, voy a eliminar el evento del botón con otro detector de eventos, pero puedo No lo hagas con removeEventListener("click",addWatchlist). he leido:

  1. ¿Por qué no funciona removeEventListener?
  2. Javascript removeEventListener no funciona
    se trata de eliminar eventos que se agregan con javascript addEventListener. Pero aquí agregamos el evento directamente en HTML.
 <button class="watch-list-btn" onclick="addWatchlist()">Watchlist </button>
 watchlist.addEventListener("click",function(){ conatinerDown.innerHTML ="" conatinerDown.innerHTML +=movieWatchlistArray.map(each=> { return each.outerHTML }).join("") // loop through movie list and changes text and removes event listener. for(let i=0 ;i<moviePageNumber;i++ ){ document.querySelector(`.movie-${i} .watch-list-btn`).textContent = "remove" document.querySelector(`.movie-${i} .watch-list-btn`).removeEventListener("click",addWatchlist) } }) // adds movie to start of an array called movieWatchlistArray function addWatchlist(){ movieWatchlistArray.unshift(document.querySelector(`.movie-${moviePageNumber-1}`)) console.log(movieWatchlistArray) }

¿Hay alguna manera de eliminar el evento? sin cambiar el HTML a algo como:

 <button class="watch-list-btn">Watchlist </button>
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Puede obtener el elemento y establecer el atributo onclick en nulo.

 function addWatchlist() { console.log('addWatchlist'); } function removeListener() { const button = document.getElementsByClassName('watch-list-btn')[0]; button.setAttribute('onclick', null); }
 <link href="https://cdnjs.cloudflare.com/ajax/libs/marx/4.0.0/marx.min.css" rel="stylesheet"/> <button class="watch-list-btn" onclick="addWatchlist()">Watchlist</button> <button onclick="removeListener()">Remove Listener</button>

about 4 years ago · Juan Pablo Isaza Report

0

<button class="watch-list-btn" id="watch-list-btn">Watchlist </button> <button class="watch-list-btn" id="remove-event">Watchlist </button>

ahora puedes escribir el javascript

 let btn = document.getElementById('watch-list-btn'); function clickHandler(){ console.log('Clicked') } btn.addEventListener('click', clickHandler); // Remove event on button click let remove = document.getElementById('remove-event'); remove.addEventListener('click', function(){ btn.removeEventListener('click', clickHandler); });

Este código funcionará para usted.

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!