Du kan använda
Kod:
document.getElementById(elementId).selectionStart
och
Kod:
document.getElementById(elementId).selectionEnd
för att plocka ut en substring som innehåller den markerade texten.
Alltså, i din kod nåt i stil med
Kod:
foundIn = document.getElementById(elementId).value.substring(document.getElementById(elementId).selectionStart, document.getElementById(elementId.selectionEnd);