Toggle INPUT value on focus


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

The markup would look like this:
<code> &lt;input class="swap-value" type="text" value="Search"/ &gt;</code>


Copy this code and paste it in your HTML
  1. swapValue = [];
  2. $(".swap-value").each(function(i){
  3. swapValue[i] = $(this).val();
  4. $(this).focus(function(){
  5. if ($(this).val() == swapValue[i]) {
  6. $(this).val("");
  7. }
  8. $(this).addClass("focus");
  9. }).blur(function(){
  10. if ($.trim($(this).val()) == "") {
  11. $(this).val(swapValue[i]);
  12. $(this).removeClass("focus");
  13. }
  14. });
  15. });

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.