javascript - Get position of selection inside html -


i need position or lets start , endoffset of selected text inside of html container (containing html). example:

<span id="text-for-selection">   <h1>heading</h1>   <p>lorem ipsum dolor sit amet, consectetuer adipiscing elit. aenean commodo ligula eget dolor.</p>   <p>aenean massa.</p>   <p>aenean massa.</p>   <p>cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. </p> </span> 

if select first "aenean massa." now, need know position of first selected letter inside text-for-selection container (including html tags!!) - not inside <p> tag. tried search it, if select second one, 1 choose?
know how selected text or how add html selection or how replace selected text. don't know how range / offset inside span container.

any ideas?

did try indexof in javascript? here example

<html id="html"> <body>  <span id="text-for-selection">   <h1>heading</h1>   <p>lorem ipsum dolor sit amet, consectetuer adipiscing elit. aenean commodo ligula eget dolor.</p>   <p>aenean massa.</p>   <p>aenean massa.</p>   <p>cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. </p> </span>   <button onclick="myfunction()">try it</button>  <script> function myfunction() { var str=document.getelementbyid('html').innerhtml;  var n=str.indexof("aenean massa."); alert(n); } </script>  </body> </html> 

this pure javascript. can use jquery also


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -