Change back colored part of text using javascript?

I have a piece of text with html tags like

<b>something</b>

      

i select a piece of text, for example "some".

with getSelection (). getRangeAt (0); I am getting the position of the cursor (textcursor), so I know which part of the text I have selected.

I have startOffset and endOffset. but the problem is that startOffset and endOffset ignore html tags, so the numbers it returns are not bad and then I don’t know which part of the text I should apply on

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

      

any solutions how to solve this? thank

+2


source to share


1 answer


<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>

      



+1


source







All Articles