i am new to jquery and i start learning jquery fundamental. i make script of jquery for registration form. it’s works fine while on submit a form with single or both field empty, query show error messages but if i again fill the empty field then also it show the same error message and on reload message stop showing.
<!DOCTYPE html>
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
<style>
c { color:red; margin:4px; }
b { color:blue; }
</style>
<script src="jquery-latest.js"></script>
<script src="jquery.js"></script>
</head>
<body>
<h1>Registration Form:</h1>
<form id="form1">
<div>First Name<input type="text" name="fname" id="fname"><span></span></div>
<div>Last Name<input type="text" name="lname" id="lname"><span></span></div>
<div><input type="submit" value="submit" ></div>
</form>
<script>
$(document).ready(function(){
$("#form1").submit(function(){
var $spans = $("span");
if ($('#fname').val()=="") {
$spans.eq(0).html("<c>Please enter your Firstname</c>");
}
if ($('#lname').val()==""){
$spans.eq(1).html("<c>Please enter your Lastname</c>");
}return false;
});
});
</script>
That’s because you do not remove the text content when input is no empty. Note that you should not laod jQuery twice and
cis not a valid tag.