<!DOCTYPE HTML><HTMLxmlns= "http://www.w3.org/1999/xhtml"><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"/> <title></title> <Scriptsrc= "Js/jquery-1.8.0.min.js"type= "Text/javascript"></Script> <Scripttype= "Text/javascript"> $(function () { //Remove the input field with the clear class //(Note: "Clear once" is a two class clear and once) $('#testform input.clear'). each (function () { //storing data by using data method $( This). Data ("txt", $.trim ($ ( This). Val ())); }). Focus (function () { //determines whether the value in the field is the same as the default value when the focus is obtained, and empties if the same if($.trim ($ ( This). Val ())=== $( This). Data ("txt")) { $( This). Val (""); }}). blur (function () { //add blur time to the domain with class clear to restore the default value //but if class is once then ignore if($.trim ($ ( This). Val ())=== "" && !$( This). Hasclass ("once")) { //Restore saved Data $( This). Val ($ ( This). Data ("txt")); } }); }); </Script></Head><Body> <formID= "Testform"> <inputtype= "text"class= "Clear"value= "Always cleared" /> <inputtype= "text"class= "Clear Once"value= "Cleared only once" /> <inputtype= "text"value= "Normal text" /> </form></Body></HTML>
jquery assigns an initial value to input and empties when focused