Reputation: 73
Displays:
Uncatched Reference error: scan not defined.
Here is the code:
<html>
<head>
<script type="text/javascript" src="../js/jquery.js"></script> <script type="text/javascript">
$(document).ready(function() {
function scan() {
$("#result").text("<font style='color: green;'>Cote</font>"); console.log("Function ran");
}
});
</script>
</head>
<body>
<font style="font-size: 84px;">Scan card, please</font>
<p id="result">cotes</p>
<input style="font-size: 36px;" id="input" maxlength=16 autofocus onkeyup="scan();">
</body>
</html>
Upvotes: 0
Views: 673
Reputation: 1650
Your scan function is only accessible in the scope of:
function() {
function scan() { ... }
}
You should place it outside this function at the level of the document:
<script type='text/javascript'>
function scan() { ... }
</script>
You should use $(document).ready for initializing the document once its content has been loaded. Over here, you are just declaring the scan function within the scope of this initialization function I've just mentioned but it is not within the scope of the event handler set at the onkeyup event. Therefore, function scan is not found.
If you have any further questions, let me know. If this solves the issue, don't forget to vote up! :)
Upvotes: 0
Reputation: 44620
scan
method definition should be outside of the other function's scope. See how you should modify your code:
function scan() {
$("#result").text("<font style='color: green;'>Cote</font>");
console.log("Function ran");
}
$(document).ready(function() {
// remove if not necessary
});
Upvotes: 1