I'm making a loading screen for a game. The user will write a password, and a link to the level is given. I would like to remove the submit button and have the function activated by pressing "enter" on the keyboard, but I am unsure how to implement it into my code.
I found this code which I imagine will work: Jquery: how to trigger click event on pressing enter key
I've tried many times using the code in that answer to make "enter" activate the submit button, but I can't get it to work. I'm pretty new to javascript and altering code like this is still confusing to me. How can I make the submit button respond to "enter" in this situation? Below is the code I am using, the JSFiddle should show its purpose clearly. If you type "level one" "level two" or "level three" you will get "correct" as a response. Anything else you will get "incorrect"
$(document).ready(function() {
$("#buttonsubmit").click(function() {
$(".all").hide("fast");
if ($('#passwordinput').val().toLocaleLowerCase() == "level one") {
$("#levelone").show("fast");
} else if ($('#passwordinput').val().toLocaleLowerCase() == "level two") {
$("#leveltwo").show("fast");
} else if ($('#passwordinput').val().toLocaleLowerCase() == "level three") {
$("#levelthree").show("fast");
} else {
$("#incorrect").show("500");
}
});
$("#passwordinput").change(function() {
$("#incorrect").hide("500");
});
});
.loadmenu {
margin-top:0%;
width:100%;
height:100%;
background:black;
position:absolute;
z-index: 17;
}
.loadtitle{
width:90%;
height:10%;
margin-left:5%;
margin-top:10%;
background:transparent;
}
.loadinput {
width:100%;
height:10%;
line-height:20px;
margin-left:5%;
margin-top:10%;
background:transparent;
}
#passwordinput{
font-size: 30px;
width:85%;
display:block !important;
}
#buttonsubmit{
margin-top:3%;
font-size: 26px;
padding:5px;
display:block !important;
}
#levelone {
display: none;
color:white;
padding-bottom:10%;
}
#leveltwo {
display: none;
color:white;
padding-bottom:10%;
}
#levelthree {
display: none;
color:white;
padding-bottom:10%;
}
#incorrect {
display: none;
color:white;
padding-bottom:10%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="loadmenu" id="loadmenu">
<div class="loadtitle">
load
</div>
<div class="loadinput">
<div class="all" id="levelone">
correct
</div>
<div class="all" id="leveltwo">
correct
</div>
<div class="all" id="levelthree">
correct
</div>
<div class="all" id="incorrect">
incorrect password
</div>
<input type='text' id="passwordinput" />
<input type='button' id="buttonsubmit" value="Submit" />
</div>
</div>