Created
January 12, 2010 13:58
-
-
Save rpheath/275218 to your computer and use it in GitHub Desktop.
This file contains 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
// Provides default text in a textbox that disappears when focused | |
// Note: also toggles an 'active' CSS class so you can change text color | |
(function($) { | |
$.fn.friendlyTextBox = function(msg) { | |
var regex = new RegExp('^' + msg + '$') | |
$(this).focus(function() { | |
if ($(this).val().match(regex)) $(this).val('') | |
$(this).addClass('active') | |
}).blur(function() { | |
if ($(this).val() == '') $(this).removeClass('active').val(msg) | |
}) | |
} | |
})(jQuery); |
This file contains 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
/* Example of how to make use of the 'active' CSS class... | |
- by default make the text light (#999) | |
- when active make the text dark (#111) */ | |
input[type=text] { | |
color: #999; | |
} | |
input.active[type=text] { | |
color: #111; | |
} |
This file contains 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
// Typical usage for something like: | |
// <input id="q" type="text" name="q" value="Enter Your Search" /> | |
$(function() { | |
$('input#q').friendlyTextBox('Enter Your Search'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment