Welcome!

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.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript ENTER key to execute function

RBBD

New Coder
I wish to enter input and hit ENTER key to execute a function that provides data back.
Not doing anything else with the mouse or keyboard. I have been trying this which does give me my data back, but I need to use the mouse and keyboard to get my data.
Try to enter "John 3:16-20" to test for the data that comes back.

HTML:
<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Document</title>

</head>

<body>

  

    <input id="input" type="text" placeholder="Enter a Bible Reference" onkeydown="printInput()" />

    <script>

        function printInput() {

            if (event.key === 'Enter') {

            const input = document.getElementById("input");

            let bob = "https://api.biblia.com/v1/bible/content/KJV.html?passage=" + input.value + "&style=fullyFormatted&key=fd16ea2e32ce50a3cb035f565fcf9f81";

            document.write('<a href="'+bob+'">Get Reference</a>');

            }

        }

    </script>

</body>

</html>
 
Last edited by a moderator:
ooooooo so so close to what you need! The only issue here is that document.write overwrites the entire page, which is why you lose the input box and everything else.

Instead, you can create an element on the page to display the link without losing your input field. Here’s an example with a &lt;div&gt; element to show the link:

Code:
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Bible Reference</title>
</head>

<body>
    <input id="input" type="text" placeholder="Enter a Bible Reference" onkeydown="printInput(event)" />
    <div id="output"></div>

    <script>
        function printInput(event) {
            if (event.key === 'Enter') {
                const input = document.getElementById("input").value;
                let url = "https://api.biblia.com/v1/bible/content/KJV.html?passage=" + input + "&style=fullyFormatted&key=fd16ea2e32ce50a3cb035f565fcf9f81";

                // Display the link in the output div
                document.getElementById("output").innerHTML = `<a href="${url}" target="_blank">Get Reference</a>`;
            }
        }
    </script>
</body>

</html>

What This Does​

  • It checks if the Enter key is pressed.
  • If so, it builds the URL and sets it as the innerHTML of a &lt;div&gt; (with the id="output"), creating a clickable link without refreshing or overwriting the page.
Give that a go, and you should get the link without needing any extra mouse or keyboard actions!
 

New Threads

Buy us a coffee!

Back
Top Bottom