Created
March 23, 2012 09:13
-
-
Save neocreo/2168787 to your computer and use it in GitHub Desktop.
JQuery: empty a specific input box using jQuery
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function input_values() | |
| { | |
| //First let's agree on which input we mean - the input-element in another element with the name "search" | |
| var inputBox = $('form#formname input, form#formname textarea'); | |
| //Now this should be triggered when we give the input focus - not click! | |
| inputBox.focus(function(){ | |
| theTemp = $(this).attr('temp'); //The temp value - if it exists | |
| var theValue = $(this).val(); //The value of the input | |
| //Now either the temp value is undefined (first time) or the temp and value are the same - 'Search' in any given language | |
| if(theTemp == undefined || theTemp == theValue){ | |
| $(this).attr('temp', theValue); //Set the temp to the same as value | |
| $(this).val(''); //Empty the value attribute to allow typing | |
| } | |
| //See, if the value is NOT equal to the temp value and the temp value is NOT undefined, it means the user has typed something. Let them keep it. | |
| }); | |
| //Finally people will click outside the box which removes the focus | |
| inputBox.blur(function(){ | |
| //if the box is empty - let's populate the value with our saved standard text in the temp attribute | |
| if ($(this).val() == "") { | |
| var theValue = $(this).attr('temp'); | |
| $(this).val(theValue); | |
| } | |
| //nothing else - if the user has typed something, let them keep it | |
| }); | |
| } | |
| input_values(); //Let's run this bad-boy |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment