-2

Apologize me as I am a beginner and I am trying to Run my keypress Event but when I press the Enter key it is not responding need help.

   Enterbtn.addEventListener("keydown",function(event){
    if(input.value.length>0 && event.keycode === 13){
    let li=document.createElement("li");
    li.appendChild(document.createTextNode(input.value));
    ul.appendChild(li);

}
  • `.keycode !== .keyCode`, and the curly brackets are unpaired! – FZs Nov 25 '20 at 18:29
  • 1
    read this https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode you prob want to use something like `event.key === 'Enter'` – George Nov 25 '20 at 18:30
  • @George That's right, but that's not the cause of the problem... – FZs Nov 25 '20 at 18:32
  • Consider using well-known IDEs with formatting and IntelliSense extensions to give you hints on syntax errors, like VSCode – Yasmin Nov 25 '20 at 18:59

3 Answers3

0

You've miswritten keyCode (capital letter)

(Edit: Maybe Enterbtn could also be a problem. Use window if you want the user to be able to trigger the event from anywhere)

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <input id="myInput" type="text" value="" />
  </head>
  <body>
    <script>

      const myInputObj = document.getElementById("myInput");

      window.addEventListener("keydown", function (event) {

        if (myInputObj.value.length > 0 && event.keyCode === 13) {
          console.log("do something");
        }
        
      });
    </script>
  </body>
</html>
RasAlGhul
  • 71
  • 1
  • 5
0

May be this could be help full

const input = document.getElementById("input");
const ul = document.getElementById("sample-ul");

input.addEventListener("keydown", function (event) {
  if (input.value.length > 0 && event.keyCode === 13) {
    console.log(event.keyCode)
    let li = document.createElement("li");
    li.appendChild(document.createTextNode(input.value));
    ul.appendChild(li);
  }
});
<input type="text" id='input' />
<ul id='sample-ul'></ul>
0

I'm not sure what you're trying to achieve but this might help you.
Enter your value and press Enter.

const myInput = document.querySelector("input");
const myList = document.querySelector("ul");

myInput.addEventListener("keydown", (e) => {
  if(e.key === "Enter") {
    const myItem = document.createElement("li");
    myItem.textContent = myInput.value;
    myList.appendChild(myItem);
    myInput.value = "";
  }
});
<!DOCTYPE html>
<html>
  <body>
    <input style="width:50%;" type="text" placeholder ="Enter your value and press 'Enter'"></input>
    <ul></ul>
  </body>
</html>
Vahid Pouyafar
  • 71
  • 1
  • 10