extender – HTMLInputElement  [ 375 views ]

Goal: give some additional function to the HTMLInputElement type

// 1. properties of the selected text (plus the original text as well)
TypeExtend(HTMLInputElement, 'selection', function() {
  var d = this;

  return {
    position: this.selectionStart,
    start: this.selectionStart,
    end: this.selectionEnd,
    length: this.selectionEnd - this.selectionStart,
    text: this.value.substring(this.selectionStart, this.selectionEnd),
    isAll: function(){var l = d.selectionEnd - d.selectionStart; return (l > 0 && d.value.length == l);}(),
    _original: {
      text: this.value,
      length: this.value.length
    }
  };
  
});

the usage is very simple:

$('.myinputbox')[0].selection() 
{
  position : 0,
  start : 0,
  end : 4,
  length : 4,
  text : 'aaaa',
  isAll : true,
  _original : {
    text: 'aaaa',
    length: 6
  }
}

in this case the inputbox value is 'aaaa' and all the text selected isAll == true
this extender working in ie as well.

#sidebar a { color:#fff; } #sidebar ul ul li { color: #DEF585; } #sidebar h2 { color: #fff; } #sidebar ul p, #sidebar ul select { color: #BEDDBE; } #backfly { background: url(images/golfBallWallPaper.jpg) left bottom fixed repeat-x #65a51d; }