jQuery: Input text default value, clear value on click, if typed value erased, get default value back


/ Published in: jQuery
Save to your folder(s)

Specifiy a default value to input text field. OnFocus, the default value clear and let you type a new value. If typed value is erased then the default value is set back.


Copy this code and paste it in your HTML
  1. <script type="text/javascript">
  2. $(document).ready(function() {
  3. swapValue = [];
  4. $("#qvtit-qvtit").each(function(i){
  5. swapValue[i] = $(this).val();
  6. $(this).focus(function(){
  7. if ($(this).val() == swapValue[i]) {
  8. $(this).val("");
  9. }
  10. $(this).addClass("focus");
  11. }).blur(function(){
  12. if ($.trim($(this).val()) == "") {
  13. $(this).val(swapValue[i]);
  14. $(this).removeClass("focus");
  15. }
  16. });
  17. });
  18. });
  19. </script>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.