PDA

View Full Version : insertAtCursor not working



PCSpectra
18 Feb 2010, 9:38 AM
I borrowed the following code from somewhere in the forums:




Ext.form.TextField.prototype.insertAtCursor =

function(v){

if (Ext.isIE) {
this.el.focus();
var sel = document.selection.createRange();


sel.text = v;
sel.moveEnd('character', v.length);
sel.moveStart('character', v.length);
}
else{
var startPos = this.el.dom.selectionStart;
var endPos = this.el.dom.selectionEnd;


this.el.dom.value = this.el.dom.value.substring(0, startPos) + v + this.el.dom.value.substring(endPos, this.el.dom.value.length);
this.el.focus();
this.el.dom.setSelectionRange(endPos+v.length,endPos+v.length);
}


While it seems to work in Firefox, IE always inserts the text at the start of the textfield.

I capture the keystroke combination CTRL+SPACE which opens a popup and lets a user select an item from a list which is then supposed to be inserted into the textarea at the point where it's last caret location was known. However when it loses focus it seems the caret location does to.

I thought maybe capture onfocus and onblur and somehow persist the caret location at least in IE but I am not sure of where to even start.

Any ideas, assistance, etc appreciated.

Cheers,
Alex