Solution 1 :

Instead of p.textContent-=e.key; do p.textContent = p.textContent.slice(0, -1);.

Problem :

How do I delete each letter the textcontent of a paragraph element at each keydown on the backspace key button like the input field remove one letter at time.

<!DOCTYPE html>
<html>
   <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width">
      <title>repl.it</title>
      <link href="style.css" rel="stylesheet" type="text/css" />
   </head>
   <body>
      <p></p>
      <script>
         let p = document.querySelector("p")
         document.addEventListener("keydown",function(e){
           if(e.key === "Backspace"){
          p.textContent-=e.key;
         }else{
         p.textContent+=e.key
          }
          })

      </script>
   </body>
</html>

Comments

Comment posted by JavaScript chop/slice/trim off last character in string

Does this answer your question?

Comment posted by ahmed Ali

Thanks brother that means alot.

Comment posted by Andre Nuechter

@ahmedAli My pleasure 🙂

Comment posted by ahmed Ali

I am making an app are you in.

Comment posted by ahmed Ali

You answered my question once I solved it about my but still thank you.

Comment posted by Andre Nuechter

@ahmedAli Is there a git repo for your app I can look up?

By