• Welcome to The Cave of Dragonflies forums, where the smallest bugs live alongside the strongest dragons.

    Guests are not able to post messages or even read certain areas of the forums. Now, that's boring, don't you think? Registration, on the other hand, is simple, completely free of charge, and does not require you to give out any personal information at all. As soon as you register, you can take part in some of the happy fun things at the forums such as posting messages, voting in polls, sending private messages to people and being told that this is where we drink tea and eat cod.

    Of course I'm not forcing you to do anything if you don't want to, but seriously, what have you got to lose? Five seconds of your life?

Just a quick javascript question.

IndigoEmmy

Comes here more often then she says she does.
Pronoun
She/Her or They/Them
How would let users input something, and based on their input, it displays certain text.

E.G if the user inputs "Fish" the word "bloop" displays, or if someone inputs "234jdfg" you get, "sorry, i didn't recognize this." or something. Does anybody know how i could do that?
 

Butterfree

Still loves Joltik, though!
Staff member
Pronoun
she/her
How do you want the user to input it? Do you just want a single-line text input field, or something else? Should the message appear when the user presses a button, or should it appear as they type?
 

IndigoEmmy

Comes here more often then she says she does.
Pronoun
She/Her or They/Them
It's a single line text input field, and then you press submit, and then it shows the answer.
 

kyeugh

Well-known member
Pronoun
she/her
what i would probably do is have a text input, a button, and an uneditable text field whose value is changed based on the text input when the button is pressed. that might look something like this:
HTML:
<input type="text" />
<button type="button" onclick="getResult()">Submit</button>
<textarea readonly></textarea>
<script>
    function getResult() {
        var inputText = document.querySelector("input");
        var outputArea = document.querySelector("textarea");
        if (inputText.value == "Fish")
            outputArea.value = "bloop";
        else if (inputText.value == "Cat")
            outputArea.value = "miau";
        else
            outputArea.value = "Sorry, I didn't recognize that.";
    }
</script>
click here for a demo. this is just how i'd do it, others might have other ideas. let me know if you have any questions or if anything is confusing.
😁
 

Butterfree

Still loves Joltik, though!
Staff member
Pronoun
she/her
Something like this:

Code:
<input type="text" id="input"> <button type="button" id="button">Submit</button>
<p id="result"></p>

<script>
document.getElementById("button").onclick = function() {
    var value = document.getElementById("input").value.toLowerCase();
    var result;
    if (value === "fish") {
        result = "bloop";
    } else if (value === "butterfree") {
        result = "free";
    } else {
        result = "Sorry, I don't recognize this";
    }
    document.getElementById("result").innerHTML = result;
};
</script>
That's going to be case-insensitive, since that's probably what you want for something like this; if that's not what you want, you'll want to remove the ".toLowerCase()".
 
Top