# AJax.InPlaceEditor - Controlling initial text selection

**URL:** <https://www.ruby-forum.com/t/ajax-inplaceeditor-controlling-initial-text-selection/147460>\
**Category:** Rails\
**Created:** [September 2, 2008, 3:14am UTC](https://www.ruby-forum.com/t/ajax-inplaceeditor-controlling-initial-text-selection/147460 "2008-09-02T03:14:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![annette](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c57346/32.png) [@annette](https://www.ruby-forum.com/u/annette)\
**Post date:** [September 2, 2008, 3:14am UTC](https://www.ruby-forum.com/t/ajax-inplaceeditor-controlling-initial-text-selection/147460/1 "2008-09-02T03:14:51Z")

</div>

I want to use in-place editing, but do not want any text to be selected  
when the user invokes the in-place editor. I would like the cursor to  
be placed at the end of the text when the in-place editor appears  
instead of having all of the text automatically selected. Is this  
possible?

Thanks,  
Annette

---

<div class="post-metadata">

**Author:** ![annette](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c57346/32.png) [@annette](https://www.ruby-forum.com/u/annette)\
**Post date:** [September 2, 2008, 4:31am UTC](https://www.ruby-forum.com/t/ajax-inplaceeditor-controlling-initial-text-selection/147460/2 "2008-09-02T04:31:44Z")

</div>

I discovered that adding the following to application.js works:

////////////////////////////////////////////////////////////////////////////////  
// Manipulates the caret position in a ‘textarea’.  
// It creates a selection if selStart and selEnd are not equal.  
//  
// To place the caret at the beginning of text:  
// “setSelRange(element, 0, 0);”  
////////////////////////////////////////////////////////////////////////////////  
function setSelRange(inputEl, selStart, selEnd)  
{  
if (inputEl.setSelectionRange)  
{  
inputEl.setSelectionRange(selStart, selEnd);  
}  
else if (inputEl.createTextRange)  
{  
var range = inputEl.createTextRange();  
range.collapse(true);  
range.moveEnd(‘character’, selEnd);  
range.moveStart(‘character’, selStart);  
range.select();  
}

inputEl.focus();  
}

////////////////////////////////////////////////////////////////////////////////  
// Extend Ajax.InPlaceEditor so that it does not automatically select  
all  
// text when the im-place editor is invoked.  
////////////////////////////////////////////////////////////////////////////////  
Object.extend(Ajax.InPlaceEditor.prototype, {  
onLoadedExternalText: function(transport) {  
Element.removeClassName(this.form,  
this.options.loadingClassName);  
this.editField.disabled = false;  
this.editField.value = transport.responseText;

```
    setSelRange(this.editField, this.editField.value.length, 

```

this.editField.value.length);  
}  
});
