I am trying to add a Class (.error_msg) only if the input with the ID "#lname" is left blank, to notify the client to fill it in. I don't want any error message when the page first loads, but it has to appear only after the client clicked on submit. I am not sure what I am doing wrong message is on the page This is what I did so far: HTML:
<div id="form_div">
<form id="form_id">
<fieldset id="fieldset">
<div>
<label for="fname">First Name:</label>
<input type="text" id="fname" name="fname">
</div>
<div>
<label for="lname">Last Name:</label>
<input type="text" id="lname" name="lname">
<p class="error_msg">You must enter a last name</p>
</div>
</fieldset>
<br>
<button type="submit" id="btn_submit">Submit</button>
</form>
</div>
The CSS:
.error_msg {
display: inline !important;
color: red;
font-size: 13px;
margin-left: 1em !important;
}
The jQuery:
$("#form_div").submit(function(evt) {
if($('#lname').val() === ''){
$("#lname").addClass("error_msg");
}
});