This article describes how to set and remove the default value of a text box using javascript and jquery. This article implements an effect similar to html5placeholder (blank prompt, for more information, see set and remove the default value of the text box. For example, when you place the cursor in the text box, the gray text disappears.
1. You can simply add the onfocus attribute to the input text box, as shown in the following code:
The Code is as follows:
Onfocus = 'if (this. value = "enter a keyword to search") {this. value = "";};'
Onblur = 'if (this. value = "") {this. value = "enter a keyword to search" ;}; '>
In fact, the onfocus attribute is quite useful. You can also change the css style through the onfocus attribute, as shown in the following code:
The Code is as follows:
Onfocus = 'if (this. value = "enter a keyword to search") {this. value = "" ;}; this. className = "input01 "'
Onblur = 'if (this. value = "") {this. value = "enter a keyword to search" ;}; this. className = "input02" '>
2. jquery can also be used for implementation:
The Code is as follows:
$ (Document). ready (function (){
Var vdefault = $ ('# keyword'). val ();
$ ('# Keyword'). focus (function (){
// When the focus is obtained, if the value is the default value, it is set to null.
If ($ (this). val () = vdefault ){
$ (This). val ("");
}
});
$ ('# Keyword'). blur (function (){
// If the value is null when the focus is lost, it is set to the default value.
If ($ (this). val () = ""){
$ (This). val (vdefault );;
}
});
});
Of course there are many ways to implement it. Here I just used it...