Insert image at current cursor position

by Rekha 2009-07-13 10:45:19

Function to insert image or smilie at the current cursor position

function addsmilie(eleId, txt)
{
var Obj = document.getElementById(eleId);
if (document.selection) // if IE
{
Obj.focus();
var selection = document.selection.createRange();
selection.text = txt;
}
else if (typeof Obj.selectionStart != 'undefined') // if Firefox
{
Obj.focus();
var startPos = Obj.selectionStart;
setCaretPosition(Obj.id, startPos);
Obj.value = Obj.value.substring(0, Obj.selectionStart) + txt + Obj.value.substring(Obj.selectionStart, Obj.value.length);
setCaretPosition(Obj.id, startPos + txt.length);
}
}

function setCaretPosition(elemId, caretPos)
{
var elem = document.getElementById(elemId);
if(elem != null) {
if(elem.createTextRange) {
var range = elem.createTextRange();
range.move('character', caretPos);
range.select();
}
else {
if(elem.selectionStart) {
elem.focus();
elem.setSelectionRange(caretPos, caretPos);
}
else
elem.focus();
}
}
}

Tagged in:

2346
like
0
dislike
0
mail
flag

You must LOGIN to add comments