hey, i have editable div , and i want to get the selected text from.
the problem was is that i want that when i press some button in the page it will wrap the selected text with something, but it dosent work because when you press on the button the selection is gone so i did onmouseup event in the div that call the function getsel(below) that save the current selection:
function getsel (s) { text = s.toString(); start = s.anchorOffset; end = s.focusOffset;}
and then when i press a my button i did this function:
function show () {div = document.getElementById('bla');div.innerHTML = div.innerHTML.slice(0, start - 1) + "<b>" + text + "</b>" + div.innerHTML.slice(end);}
its work great, but only with something like BBcode,because the getselection object refers to the selected text only without the html inside him, for example if i have in my div the next text: "Hello World";
and now i will select the word "Hello" and bold it with my function it work great, but now if i want to bold: "World", there is a problem, because the location of the start and the end that i get from the getselection object match to the string without the html, and the innerHTML including the html tag and then when i use the location in slice method its not match to the innerHTML text.
what can i do?