Wikipedia Viewer Help

Hi! I’m trying to make my search form work on Enter click and save the input in the variable so it can be later used in API request. But my code doesn’t seem to work, and I’m not sure what’s wrong. Here’s my code so far (I haven’t started on the API part yet, just getting the input):

$(document).ready(function(){
  
  $("#input1").keyup(function(event){
    if(event.KeyCode === 13){
      $("#input1").click();
    }
  $("#input").on("click", function(){
          var text = $("input1").val();
           console.log(text);
  })
  })
});

The HTML part looks like this:

<p><input type = text id = input1></p>

Where does the $("#input") come from (I don’t see an element with #input. )? also var text = $("input1").val(); misses a #: var text = $("#input1").val();

my bad, “#input” should’ve been “#input1”. fixed it, also fixed ```
var text = $("#input1").val();

but it still doesn't work.

KeyCode should be keyCode. Also you should first close the keyup event and than open the click event.

You have quote marks missing from your html attributes.

Ah, that as well. Codepen is quite forgiving though.

I forked your pen and made a few changes to the syntax. This works: https://codepen.io/Malgalin/pen/jMgdeG?editors=0011

Edit

…Sorta works…it console logs multiple times for some reason…

That’s because the click handler is set inside of the keyup event. So every time a keyup event happens a new click event handler is added to $("#input1").

Oh, yeah. I feel silly. That was obvious :blush:

1 Like

Thank you all so much, it works now! And also thanks for reminding me about quotation marks in html)