Check this Link
This is a jquery plugin and this might help you in achieving your result.
I have used some string functions and setSelectionRange in order to select the text inside a text box.
HTML
<input type="button" value="Select" onmousedown="selectCup(); return false">
<input type="text" id="select_text">
Js
function selectCup() {
var search_text = document.getElementById("select_text").value;
var n = search_text.length;
var input = document.getElementById("test");
var input_text = input.value;
var x = input_text.indexOf(search_text);
var y = x+n;
input.focus();
input.setSelectionRange(x, y); // Highlights "Cup"
};
I have udated in this fiddle