I am trying to make a message appear on the viewport only once all text areas are filled. Right now myMessage appears immediately upon clicking the button on my page even though the first name field has yet to be validated. How do I make the myMessage ONLY appear once the form element for "First Name" is filled out by the user?
$(document).ready(function() {
$("#submitinfo").click(function() {
var myFirstName = $("#first_name").val().trim();
if (!myFirstName) {
$("#first_error").html("You must Enter a First Name");
$('#first_name').focus();
} else {
$("#first_error").html("");
}
$("#message").css('background-color', 'red');
var myMessage = "Your name is " + myFirstName;
$("#message").html(myMessage);
return false;
});
});
<label for="first_name">First Name:</label>
<input type="text" id="first_name" name="first_name"> <span class="error" id="first_error"></span>
<input type="button" id="submitinfo" value="Submit">
<span class="message" id="message"></span>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>