title: JavaScript find the index of a selected character in a textbox
body: <p>I have a textbox with a 2-digit repeated number, (for example 11), and when I highlight the first digit, is there any functions that can return me an index of 0?
  What I tried is that I have: </p>
  
  <pre><code>    var selectedString = window.getSelection();
      if (selectedString == textBoxValue.charAt(0)) {...}
  </code></pre>
  
  <p>This won't work because the selected string has the value "1" from the first digit of 11, so it will go into the if statement, but if I only selected the second digit, which selectedString will also be "1" and equals to textBoxValue.charAt(0), but I don't want that to happen because the second digit should correspond to textBoxValue.charAt(1).</p>
  
  <p>So is there a way to get the index of the highlighted value, so I can do something like:</p>
  
  <pre><code>    if (selectedString == textBoxValue.charAt(0) &amp;&amp; selectedString.index == 0) {...}
  </code></pre>