Changeset 9e2c949 in github


Ignore:
Timestamp:
Mar 15, 2012 5:55:22 PM (15 months ago)
Author:
thomascube <thomas@…>
Branches:
master, HEAD, courier-fix, dev-browser-capabilities, pdo, release-0.8
Children:
f45a9789
Parents:
b9ce92d
Message:

Improve input field placeholders: use native attributes if supported, encapsulate in jquery plugin

Location:
program/js
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • program/js/app.js

    rb9ce92d r9e2c949  
    45064506      elem = $('.ff_' + col); 
    45074507 
    4508     elem.focus(function(){ ref.focus_textfield(this); }) 
    4509       .blur(function(){ ref.blur_textfield(this); }) 
    4510       .each(function(){ this._placeholder = this.title = (ref.env.coltypes[col].label || ''); ref.blur_textfield(this); }); 
     4508    elem.placeholder(ref.env.coltypes[col].label); 
    45114509  }; 
    45124510 
     
    54235421      } 
    54245422    } 
    5425   }; 
    5426  
    5427   this.focus_textfield = function(elem) 
    5428   { 
    5429     elem._hasfocus = true; 
    5430     var $elem = $(elem); 
    5431     if ($elem.hasClass('placeholder') || $elem.val() == elem._placeholder) 
    5432       $elem.val('').removeClass('placeholder').attr('spellcheck', true); 
    5433   }; 
    5434  
    5435   this.blur_textfield = function(elem) 
    5436   { 
    5437     elem._hasfocus = false; 
    5438     var $elem = $(elem); 
    5439     if (elem._placeholder && (!$elem.val() || $elem.val() == elem._placeholder)) 
    5440       $elem.addClass('placeholder').attr('spellcheck', false).val(elem._placeholder); 
    54415423  }; 
    54425424 
  • program/js/common.js

    r43d98b2 r9e2c949  
    695695 
    696696// Make getElementById() case-sensitive on IE 
    697 if (bw.ie) 
    698 { 
     697if (bw.ie) { 
    699698  document._getElementById = document.getElementById; 
    700   document.getElementById = function(id) 
    701   { 
     699  document.getElementById = function(id) { 
    702700    var i = 0, obj = document._getElementById(id); 
    703701 
     
    709707  } 
    710708} 
     709 
     710// jQuery plugin to emulate HTML5 placeholder attributes on input elements 
     711jQuery.fn.placeholder = function(text) { 
     712  return this.each(function() { 
     713    var elem = $(this); 
     714    this.title = text; 
     715 
     716    if ('placeholder' in this) { 
     717      elem.attr('placeholder', text);  // Try HTML5 placeholder attribute first 
     718    } 
     719    else {  // Fallback to Javascript emulation of placeholder 
     720      this._placeholder = text; 
     721      elem.blur(function(e) { 
     722        if ($.trim(elem.val()) == "") 
     723          elem.val(text); 
     724        elem.triggerHandler('change'); 
     725      }) 
     726      .focus(function(e) { 
     727        if ($.trim(elem.val()) == text) 
     728          elem.val(""); 
     729        elem.triggerHandler('change'); 
     730      }) 
     731      .change(function(e) { 
     732        var active = elem.val() == text; 
     733        elem[(active ? 'addClass' : 'removeClass')]('placeholder').attr('spellcheck', active); 
     734      }); 
     735 
     736      if (this != document.activeElement) // Do not blur currently focused element 
     737        elem.blur(); 
     738    } 
     739  }); 
     740}; 
     741 
    711742 
    712743// This code was written by Tyler Akins and has been placed in the 
Note: See TracChangeset for help on using the changeset viewer.