Set default values for input tags using jQuery
- 2021-06-29 09:48:45
- OfStack
Due to project requirements, a simple input default setting was written to implement the method of setting default values to all inputs in.form.
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>input Default Value Settings </title>
<script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
// Collective Call
$(".form input").each(function(){
$(this).setDefauleValue();
});
// Single Call
$("#key").setDefauleValue();
})
// Set up input,textarea Default value
$.fn.setDefauleValue = function() {
var defauleValue = $(this).val();
$(this).val(defauleValue).css("color","#999");
return this.each(function() {
$(this).focus(function() {
if ($(this).val() == defauleValue) {
$(this).val("").css("color","#000");// Color of input value
}
}).blur(function() {
if ($(this).val() == "") {
$(this).val(defauleValue).css("color","#999");// Default value color
}
});
});
}
</script>
</head>
<body>
<form class="form">
<input type="text" size="30" value=" Enter a nickname ">
<br>
<input type="text" size="30" value=" Enter Name ">
</form>
<br>
<br>
<br>
<input type="text" size="30" id="key" value=" Input Student ID , name, nickname ">
</body>
</html>