Home

Tags

Вставка текста в textarea в позицию курсора

2010-07-16 web javascript html

function insertAtCursor(obj, val) {
	//IE support
	if(document.selection){
		obj.focus();
		sel = document.selection.createRange();
		sel.text = val;
	} else //MOZILLA/NETSCAPE support
	if (obj.selectionStart || obj.selectionStart == '0') {
	var startPos = obj.selectionStart;
	var endPos = obj.selectionEnd;
	obj.value = obj.value.substring(0, startPos) + val + obj.value.substring(endPos, obj.value.length);
	} else obj.value += val;
	return false;
}

Использование
<forum name="formedit"><textarea name="content"></textarea></form>
<a href="#" onclick="return insertAtCursor(document.formedit.content,'8')">8</a>

comments powered by Disqus