Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

JavaScript Enter key input result disappears

Regy

Active Coder
I've got a problem with my code. I can detect the Enter key press in an input text element (or whatever it's called) but I only get to see the result briefly and then it disappears.
So my question is, why is this happening?
Thanks.
HTML:
<!DOCTYPE html>
<html>
<body>
<style>
#myresult
{border: 1px solid;min-height: 100px;width: 500px;text-align: left;}
</style>
<form id="myform">
<input id="myinput" type="text" onkeypress="clickPress(event)">
</form>
<script>
function clickPress(event) {
    if (event.key == "Enter") {
    document.getElementById("myresult").innerHTML = "You pressed the Enter key."
    }
}
</script>
<br>
<p id="myresult" ></p>
</body>
</html>
 
Because the form is submitted by pressing this key.

Oh okay. If necessary, I'll read more about forms later on.

You know, after reading your post/link (thank you for the javascript code - which I did try out) I was thinking 'Do I really need a form element if I've only got one input box?' So I tried it and it worked....
HTML:
<!DOCTYPE html>
<html>
<body>
<style>
#myresult
{border: 1px solid;min-height: 100px;width: 500px;text-align: left;}
</style>
<input id="myinput" type="text" onkeypress="clickPress(event)">
<script>
function clickPress(event) {
    if (event.key == "Enter") {
    document.getElementById("myresult").innerHTML = "You pressed the Enter key."
    }
}
</script>
<br>
<p id="myresult" ></p>
</body>
</html>

But if I don't include this form element, would that cause problems in the future if I decide to do server side stuff like mySQL and PHP?

Thanks again Padonak 🙂
 
Oh okay. If necessary, I'll read more about forms later on.

You know, after reading your post/link (thank you for the javascript code - which I did try out) I was thinking 'Do I really need a form element if I've only got one input box?' So I tried it and it worked....
HTML:
<!DOCTYPE html>
<html>
<body>
<style>
#myresult
{border: 1px solid;min-height: 100px;width: 500px;text-align: left;}
</style>
<input id="myinput" type="text" onkeypress="clickPress(event)">
<script>
function clickPress(event) {
    if (event.key == "Enter") {
    document.getElementById("myresult").innerHTML = "You pressed the Enter key."
    }
}
</script>
<br>
<p id="myresult" ></p>
</body>
</html>

But if I don't include this form element, would that cause problems in the future if I decide to do server side stuff like mySQL and PHP?

Thanks again Padonak 🙂
Best practice is to use form element for anything server-side, so unfortunately, yes, you're still stuck with it
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom