0

I have 1 textbar and i want to get the value from the user with Enter key on the keyboard and then a function run (the name of my function is newNote). my code is this, right?

function eventListeners(){
  document.querySelector("#note").addEventListener("keypress", function (e){
      if (e.key === "Enter"){
          newNote();
      }
  })
}
Pawara Siriwardhane
  • 1,873
  • 10
  • 26
  • 38
Amir Rezaie
  • 43
  • 1
  • 11
  • 4
    Whats the question>? – 0stone0 Jun 19 '21 at 15:57
  • the question clearly lacks context. You should also consider using event delegation – Mister Jojo Jun 19 '21 at 16:10
  • Welcome to SO. You might find reading the site [help section](https://stackoverflow.com/help) useful when it comes to [asking a good question](https://stackoverflow.com/help/how-to-ask). To get the best answers to your question we like to see that you've attempted to solve the problem yourself first using a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). Here's a [question checklist](https://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist) you might find useful... – Mister Jojo Jun 19 '21 at 16:10
  • You can use Jquery for this. For Reference - https://stackoverflow.com/questions/979662/how-to-detect-pressing-enter-on-keyboard-using-jquery – Rudr Thakur Jun 19 '21 at 16:39

2 Answers2

-1

I try and find the right answer:

function nameOfYourEventListener(){
document.querySelector("#yourInputID").addEventListener("keydown", function (e){
if (e.code === 'Enter'){
    newNote(e); //function that you run for add the content to the list
}});
Amir Rezaie
  • 43
  • 1
  • 11
-2

Solution


function eventListeners(){

let targetInput = document.querySelector('#inputId');

document.querySelector("#note").addEventListener("keypress", function (e){
    if (e.key === "Enter"){
        
         let value = target.value; // do something with the value

        newNote();
    }
})

}
  • Please don't answer questions with pure code. Take a look at [answer]. Adding some details on what you've changed would be a good start. – 0stone0 Jun 19 '21 at 16:01
  • Also, what's the use of `targetInput`? – 0stone0 Jun 19 '21 at 16:02
  • Okay apologies for not explaining. the `targetInput` is a variable that holds the HTML input that the user typed in some value you can have it assign an id then retrieve the value via `targetInput` or textbar – OBI PASCAL BANJUARE Jun 19 '21 at 16:06