Posted By


terrencewood on 07/31/10

Tagged


Statistics


Viewed 440 times
Favorited by 1 user(s)

jquery.defaultvalue


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



Copy this code and paste it in your HTML
  1. /**
  2.  * Remove and restore the defaultValue from a text input or textarea if it is unchanged.
  3.  */
  4. (function ($) {
  5. $.fn.defaultvalue = function () {
  6. var els = $(this).filter("textarea,input[type=text]");
  7. return els.each(function () {
  8. var el = $(this);
  9. var dv = el.attr("defaultValue");
  10. el.focus(function () {
  11. if (el.attr("value") == dv) {
  12. el.attr("value", "")
  13. }
  14. }).blur(function () {
  15. if (el.attr("value") == "") {
  16. el.attr("value", dv)
  17. }
  18. })
  19. })
  20. }
  21. })(jQuery);

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.