Before I begin I'd like to state that I'm a total beginner to HTML/JS/web development in general. Please keep this in mind when you look at my code! I'm currently building a simple website where the user inputs some text, which generates a bunch of follow-up text related to the input. I used JavaScript to display the input and the follow-up text once the 'submit' button has been pressed. What I'd like to do now is create an if/else statement that decides which follow-up text will display based on the input; so for example, if the user typed in a specific phrase, the follow-up text which relates to the phrase would also display on the page; but if the user typed in another specific phrase, there would be another block of follow-up text that relates to the second specific phrase.
However, when I try to write an if/else statement, the functions that I previously defined aren't being called within the statement. I figure this is to do with the scope of the functions, but I don't know how to fix that issue. It's also entirely possible that my code is bad or I made some mistake, but I've reached a bit of a dead end, which is why I'm turning to you guys!
<script language="JavaScript">
//onClick allows the Input and the follow up text to post at the same time
function onClick() {
showInput();
antiEuropeExample();
}
//showInput returns the user input (check the HTML below)
function showInput() {
document.getElementById("display").innerHTML = document.getElementById(
"user_input"
).value;
}
//Insane hard coding here, I'm so sorry
function antiEuropeExample() {
document.getElementById("twt_1").innerHTML =
"I hate the EU! We need to get rid of it. ";
document.getElementById("twt_2").innerHTML =
"I hope Europe sinks into the sea.";
}
//this if-else statement isn't working cos of scope issues i guess??? i want an if/else statement to decide which block of code should execute depending on what the user types into the input.
/*
function onClick() {
if ((showInput = "I hate the EU!")) {
antiEuropeExample;
} else {
alert("It worked!");
}
}
*/
</script>
NOTE: The first 'onClick' function works but please ignore it for the sake of this question; the second 'onClick' function that is commented out is the one that I'm having problems with. Also if anyone has any tips to clean up my syntax that would be super appreciated! Thank you!
Aucun commentaire:
Enregistrer un commentaire