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

61
Views
Delete Elements Created by Event Listener

I'm trying to override an eventlistner so the accumulated text on the screen under "Message Will Add Here" disappears and I can enter new text whenever the user clicks ok in the alert window. But whatever method I use for this doesn't do anything. Can you please tell me what I'm doing wrong? Here is my code:

HTML

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
    <div class="btncontainer">
      <button class="button"type="button" name="button">
        Over or Click Here
      </button>
      <p>MESSAGE WILL ADD HERE</p>
    </div>
    <script type="text/javascript" src="testc.js">
    </script>
  </body>
</html>

Javascript

var btn = document.querySelector('.button');

var count = 1;
var newpar;
var newval;
var container = document.getElementsByClassName('.btncontainer');

function e(){
  newpar = document.createElement('p');
  newpar.className = 'ps'
  newval = document.createTextNode('Was hovered ' + count + ' times');
  newpar.appendChild(newval);
  document.querySelector('.btncontainer').appendChild(newpar);
  count++;
}
btn.addEventListener('mouseover', e);

btn.addEventListener('click',function(){
  count-=1;
  alert(`The button was hovered over ${count} times`);
  count = 0;
  container.removeChild(newpar);
  btn.removeEventListener('mouseover', e);

})

Result

about 4 years ago · Juan Pablo Isaza
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!