|
|
Insert image at current cursor position - Javascript
|
Views : 1172
|
|
Tagged in : Javascript
|
|
|
Report This Scrap as Inappropriate We request you to choose the appropriate categroy and subcategory that suits your
objectionable concern about the scrap, So that our team can review and find out whether it violates our Guidelines or the
scrap is not suitable for all viewers.
|
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();
}
}
}
|
|
By Rekha, On - 2009-07-13 |
|
|
|