/ Published in: jQuery
This is a jQuery custom function used for clearing the default input values of input fields without having to add a class or inline javascript like onclick or onblur. Just write your input and give it a value like you normally would and then add this function and you're good to go.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
$(document).ready(function(){ jQuery.fn.inputtoggle = function(){ $(this).each(function(index){ var myvalue = $(this).attr("value"); $(this).focusin(function(){ if($(this).val() == myvalue) $(this).val(""); }); $(this).focusout(function(){ if($(this).val() === "") $(this).val(myvalue); }); }); }; //call custom inputtoggle() method on all inputs //you could also be more specific, and write: $("input[type=text]").inputtoggle(); $("input").inputtoggle(); //close jquery }); html: very basic ... <input type="text" name="input_name" value="Your Value Name" />