Took me a couple of hours to find this out. Couldn’t find it on the internet. With a little bit of improvising finally found out the way.
var range = document.getElementById('iframe_id').contentWindow.getSelection().getRangeAt(0);
Since this is an iframe we are talking about you should know the startOffset and endOffset is wrt to the container of the start pos and end pos text. In here what I meant by the container is that iframe may contain html tags.
...my name is <b>saminda</b>. I like js.....
if you had selected the text “is samin” from the iframe the endOffset would be 5, not startOffset+3+5. Hope you understood