0

My code won't work. It's supposed to alert whenever a user presses a key while focused inside of a textbox element!

$("#yourcode").keyup(function() { alert("I found Hogwarts."); }); 
1
  • what's the HTML look like, Harambe Squad Commented Mar 26, 2017 at 0:31

1 Answer 1

2

You need to wait until the DOM is ready before adding the keyup handler. This can be achieved by calling .ready() after using $() to get a DOM reference to the document (i.e. $(document)). Also make sure you load jQuery before you load the script:

JS:

<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script> <script> $(document).ready(function(){ $("#yourcode").keyup(function() { alert("I found Hogwarts."); }); }); </script> 

HTML:

<textarea id="yourcode"></textarea> 

Here is a working example (also available in this jsFiddle):

$(document).ready(function(){ $("#yourcode").keyup(function() { alert("I found Hogwarts."); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <textarea id="yourcode"></textarea>

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.