Attention: We are retiring the ASP.NET Community Blogs. Learn more >

TextRange object

Today, while fine-tuning an application that I'm writing for a friend of mine I stumbled when I had to implement a certain feature.  Basically, there's a textbox which contains a big string which is a combination of smaller strings; each smaller string represents a feature on a motor vehicle.  So, given the following string:

60DP2D75FE1GU4JL9LN3M30N40N46PDEPG1QAHRHDUJ8

It could be that "60D" represents "has airbag" and "P2D75E1" represents "Limited Slip Diff." and so on.   The client wanted me to implement a feature that would allow them to enter a string and have it highlighted with the Textbox if found. 

So, if the user entered “9LN3M“ the TextBox would be highlighted as so:

60DP2D75FE1GU4JL9LN3M30N40N46PDEPG1QAHRHDUJ8

The solution only has to work in IE, so, after a bit of searching I came across the TextRange object.  The following chunk of code worked a treat... very nice!

function FindInTextBox()
{
    var e = document.getElementById( optionStringTextBox ) ;
    var bDisabled = e.disabled ;
        
    var find = prompt( "Search option string for a particular value.", "" ) ;
    var r = e.createTextRange() ;
    if( r.findText( find ) )
    {
        e.disabled = false ;
        r.select() ;
        e.disabled = bDisabled ;
    }
}

No Comments