The example for finding the value of an input box in jQuery is below.
<!DOCTYPE html>
<html>
<head>
<style>
p { color:blue; margin:8px; }
</style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<input type="text" value="some text"/>
<p></p>
<script>
$("input").keyup(function () {
var value = $(this).val();
$("p").text(value);
}).keyup();
</script>
</body>
</html>
I have 2 questions.
- Why is the script inside the body? (I tried putting it between the head tags and it does not work).
- Why are there two different keyup() methods, one directly after $(“input”) and the other after the function?
.ready()or.load()(usually.ready()).keyup()fires the eventhttp://jsfiddle.net/eqfbY/
Ready waits until the DOM is ready to be parsed. Otherwise, what you’re running is an inline script run in the order it is parsed.
Note, a shorthand version of
ready()is: