As I was experimenting with a basic program that turns user input into an alert upon submission, I encountered an interesting issue. It seems that the program only behaves as expected when using 'false' as the first condition in my if/else statement, rather than 'true'. Despite my efforts to troubleshoot and research this problem, I couldn't find any relevant information. Therefore, I've decided to reach out here for assistance. Any help provided would be greatly appreciated.
Below is the HTML snippet:
<form id="greetingForm">
<input type="text" name="userInput" id="userInput"/>
<input type="submit" value="click" id="submit"/>
</form>
The original script that isn't functioning correctly:
function output(){
var input = document.getElementById('userInput').value;
if(input == true){
alert(input);
}else{
alert('Say something!');
}
}
function init(){
var greetingForm = document.getElementById('greetingForm');
greetingForm.onsubmit = output;
}
window.onload = init;
An updated version of the script that works properly:
function output(){
var input = document.getElementById('userInput').value;
if(input == false){
alert('Say something!');
}else{
alert(input);
}
}
function init(){
var greetingForm = document.getElementById('greetingForm');
greetingForm.onsubmit = output;
}
window.onload = init;