korzystając z gotowej funkcji insertAtCursor napisałem funkcje która dodaje mi do textarea listę numerowaną. Wszystko ładnie działa tylko chciałbym żeby to dodawało w czasie realnym (tak jak jest w IPB). Niestety nie wiem jakby się za to zabrać :/ Proszę o pomoc.
Kod
function insertAtCursor(txtarea, open, close, newTXT) {
if (document.selection) {
txtSel = (document.selection.createRange().text) ? document.selection.createRange().text : newTXT;
txtarea.focus();
sel = document.selection.createRange();
sel.text = open + txtSel + close;
} else if (txtarea.selectionStart || txtarea.selectionStart == '0') {
var selLength = txtarea.textLength;
var selStart = txtarea.selectionStart;
var selEnd = txtarea.selectionEnd;
strSelect = txtarea.value.substr(txtarea.selectionStart, txtarea.selectionEnd - txtarea.selectionStart)
txtSel = (strSelect != '') ? open + strSelect + close : open + newTXT + close;
newTXT = txtSel;
var startPos = txtarea.selectionStart;
var endPos = txtarea.selectionEnd;
txtarea.value = txtarea.value.substring(0, startPos)+ newTXT + txtarea.value.substring(endPos, txtarea.value.length);
} else {
txtarea.value += newTXT;
}
}
var list = "[ol]\n";
function list_ordered() {
var element;
element = prompt('Wprowadź element listy:');
if(element) {
list += "[li]" + element + "[/li]\n";
list_ordered();
} else {
insertAtCursor(document.getElementById('tresc'), list, '[/ol]', '');
list = "[ol]\n";
}
}
if (document.selection) {
txtSel = (document.selection.createRange().text) ? document.selection.createRange().text : newTXT;
txtarea.focus();
sel = document.selection.createRange();
sel.text = open + txtSel + close;
} else if (txtarea.selectionStart || txtarea.selectionStart == '0') {
var selLength = txtarea.textLength;
var selStart = txtarea.selectionStart;
var selEnd = txtarea.selectionEnd;
strSelect = txtarea.value.substr(txtarea.selectionStart, txtarea.selectionEnd - txtarea.selectionStart)
txtSel = (strSelect != '') ? open + strSelect + close : open + newTXT + close;
newTXT = txtSel;
var startPos = txtarea.selectionStart;
var endPos = txtarea.selectionEnd;
txtarea.value = txtarea.value.substring(0, startPos)+ newTXT + txtarea.value.substring(endPos, txtarea.value.length);
} else {
txtarea.value += newTXT;
}
}
var list = "[ol]\n";
function list_ordered() {
var element;
element = prompt('Wprowadź element listy:');
if(element) {
list += "[li]" + element + "[/li]\n";
list_ordered();
} else {
insertAtCursor(document.getElementById('tresc'), list, '[/ol]', '');
list = "[ol]\n";
}
}