Wikipedia Viewer Help

Wikipedia Viewer Help
0

#1

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>

#2

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();


#3

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

but it still doesn't work.

#4

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


#5

You have quote marks missing from your html attributes.


#6

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


#7

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…


#8

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").


#9

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


#10

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