I have seen many questions asked about ': focus', but have not seen many helpful answers with context to text-based games or using inside of an if statement. I wanted to be sure what I was writing was correct, and I don't get any errors from my code, I just simply don't get a return. I want to code this game very badly, but sadly can't start until I get past this hump! Thanks for the help in advance!!
I also found this question, https://https://stackoverflow.com/questions/967096/using-jquery-to-test-if-an-input-has-focus?rq=1. It asked and it looks like I followed what the answer says, but the error code still points to something being off on that line of code.
This is my HTML doc, but I'm almost positive there isn't anything wrong here.
<!DOCTYPE HTML>
<head>
<title>Zork!!</title>
</head>
<body>
<div id = "game text"><p>Welcome to Zork!!</div>
<input id = "user-input" placeholder="Please type your command.."></input>
<script type="text/javascript" src="jquery-3.4.1.min.js"></script>
<script type="text/javascript" src="script.js"></script>
</body>
This is the original 'script.js' I had before changing to the answer:
$(document).ready(function(){
$(document).keypress(function(key){
if(key.which === 13 && $("user-input").is("*:focus")){
//I also used "input: focus" and got the same error code
var value = $("user-input").val();
alert(value);
}
})
})
With what I have, I THOUGHT I would be able to type words into the input box hit enter, and get a text box saying back to me whatever I typed in.
error code :
jquery-3.4.1.min.js:2 Uncaught Error: Syntax error, unrecognized expression:
: focus
at Function.se.error (jquery-3.4.1.min.js:2)
at se.tokenize (jquery-3.4.1.min.js:2)
at se.compile (jquery-3.4.1.min.js:2)
at se.select (jquery-3.4.1.min.js:2)
at se (jquery-3.4.1.min.js:2)
at Function.se.matchesSelector (jquery-3.4.1.min.js:2)
at Function.k.filter (jquery-3.4.1.min.js:2)
at j (jquery-3.4.1.min.js:2)
at k.fn.init.is (jquery-3.4.1.min.js:2)
at HTMLDocument.<anonymous> (script.js:7)
//This is the line pointing to the :focus issue I was having
Thank you Amit and Ibrahim!