Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Highlighting text in Textbox

Status
Not open for further replies.

790213

Programmer
Sep 22, 2003
50
ZA
Do you know how I can select only a part of the text in a textbox? Eg. If the textbox has the word - Hello World then I want to select Hello or World or just the W.
 
I do not know the answer but maybe this will be of use? It works for words within the body element only... so you might want to use it with an <iframe> :(

var a = document.body.createTextRange();
var b = a.findText('hello world');
 
790213,

stombind, you were right on target..
using your keywords found this:

Code:

My testing shows this does not work in NN7
But it does work in IE6
Code:
<html><head><title>TEST</title>
<script language=&quot;JavaScript&quot;>
function txtsel(str){
obj = document.getElementById('txt');
if(document.all && (obj.value.indexOf(str)> -1)){
seltxt = document.getElementById('txt').createTextRange();
seltxt.findText(str);
seltxt.select(); }
}
</script></head><body>
<form name=&quot;test&quot;>
<textarea id=&quot;txt&quot; rows=&quot;6&quot; cols=&quot;12&quot;>This is a TEST
</textarea><p>
Enter Search:<input type=&quot;text&quot; id=&quot;find&quot; size=&quot;20&quot;
 onBlur=&quot;txtsel(this.value);&quot;>
</body></html>

2b||!2b
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top