views:

134

answers:

1

i have some part of text with html tags , for example

<b>something</b>

i select some part of text for example "some".

with getSelection().getRangeAt(0); i get position of caret (textcursor), so i know which part of text i've selected.

i have startOffset and endOffset. but problem is, that startOffset and endOffset ignores html tags, so numbers which it returns are not bad, and then i don't know on which part of text i have to apply

<span style="background-color: somecolor ">some</span>

any ides how to solve this ? thanks

+1  A: 
<b id='str1'>something</b>
<script>
function jsReplace()
{
  var elem = document.getElementById('str1')
  elem .innerHTML = elem .innerHTML.replace('some', '<span style="background-color: somecolor ">some</span>')

}
</script>
Kheu
yes, but what will you do , if there isn't id in that b tag ?
mm
how do you get your string and from which control?
Kheu
with selection and range, user selects text with mouse
mm
ok use getSelection()function jsReplace(){ var str= document.getSelection() /*supposing it's ie, u could try window.getSelection and document.selection*/ str=str.replace('some', '<span style="background-color: somecolor ">some</span>')}and apply the function onmousedown
Kheu