Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use the setSelectionRange (mozilla/webkit) or selection.createRange (IE) methods to find the currently highlighted text inside a textarea. I put up an example on <a href="http://jsfiddle.net/niklasvh/2YNpJ/" rel="nofollow">jsfiddle</a>, but have commented out your regexp since it hangs the browser in many instances. You need to make it more restrictive, and it currently passes a lot of other things than youtube url's as well. </p> <p>However, the example has a working solution how to get the currently selected text, which you can, after fixing your pattern, apply to the idPattern.exec().</p> <pre><code>idPattern = /(?:(?:[^v]+)+v.)?([^&amp;=]{11})(?=&amp;|$)/; // var vidId = prompt("YouTube Video", "Enter the id or url for your video"); var vidId; el = document.getElementById('texty'); if (el.setSelectionRange) { var vidId = el.value.substring(el.selectionStart,el.selectionEnd); } else if(document.selection.createRange()) { var vidId = document.selection.createRange().text; } alert(vidId); </code></pre> <p>EDIT: Wrapping the highlighted text and outputting it back to the element. <a href="http://jsfiddle.net/2YNpJ/51/" rel="nofollow">example</a></p> <pre><code>el = document.getElementById('texty'); if (el.setSelectionRange) { el.value = el.value.substring(0,el.selectionStart) + "[highlight]" + el.value.substring(el.selectionStart,el.selectionEnd) + "[/highlight]" + el.value.substring(el.selectionEnd,el.value.length); } else if(document.selection.createRange()) { document.selection.createRange().text = "[highlight]" + document.selection.createRange().text + "[/highlight]"; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload