How to read text selected anywhere on a web page in IE8

I have an app that generates tables of data for the user to look at. Some of the data shown in some columns are keys, such as CUSIP numbers, Tickers or other unique identifiers. I want the user to be able to highlight one of these numbers and be able to click something on the page that let's them look up the meaning of that number in a table.

So, let's say the app can display CUSIPs, customer IDs, Tickers, and Curve Names, whatever those are. I would like to create a feature where the user can select some text on the page and can then click a drop-down-list with choices of "Look up as CUSIP", "Look up as Customer ID", etc. When the user selects look up as CUSIP, some JavaScript would fire that would read the selected text and do the lookup, replacing the page or popping up the information.

The only part I do not know how to do is find out what text the user has selected on the page. I do not know which element the will have clicked on.

I can wrap whatever I want around stuff to make this easier, as I have total control over the HTML that makes up the page.

Unfortunately, this has to work in IE8, because that is what we are using at work, and I would like it to keep working after that, but I realize I may have to code another solution if we upgrade to a modern browser.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rainer JeschorCommented:
does it have to be selected text?
Idea would be to wrap the texts inside SPAN tags and additionally add a custom attribute or class to it (so that you have both the text and the context like customer no or CUSIP). Then you can use e.g. jQuery to dynamically attach the click event to the class as well as adding small icons after the span through the CSS to emphasize that the text has "a lookup" and is clickable.

Pseudo sample:

Just my 2ct
jkurantAuthor Commented:
Rainer, I really appreciate your thinking outside the box! I love the idea of using CSS to indicate that the text that people might want to look up can be looked up. The problem is that the app is a front-end to a database and the database can change, so I don't know which fields in which tables a user might want to look up. So, really any text on the page could need to be looked up. So, I figured the only way for the user to specify which text they want to look up is to have them select that text. I wish I could think of another way. If I wrap every bit of text in that span you suggested, then I am not sure but maybe I can capture a right-click on a span or a double-click, and then offer to look up whatever text is in the span tag. But that is still not perfect, as the user might want to leave off part of the text in the cell. Sometimes there are concatenated IDs in the the fields and the user would only want to look up one. Example, "Deal #12345 in Portfolio #9876 was not found".

But what about looping through all the elements on a page and checking to see if text is selected therein? Is that possible in IE8?

I did try setting up a drop target and dragging and dropping selected text onto an image or div, but I could never get that to work. Maybe that is where I should direct my efforts. Basically, I need some way for the user to identify text to be looked up and then a way to trigger the lookup, which can be sophisticated from that point forward (new dialog etc.)
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
Maybe it would help if you can set up some test front end html.

My take would be clicking on a column and either by the class or perhaps the column number, determine what type data it is, then grab the text in the table <td> and submit that to an ajax page for look up and return.,output
<!DOCTYPE html>
<script src="//"></script>
  $('.get td').on('click',function(){
    var c=$(this).attr('class');  // type of data
    var d=$(this).text(); // data to send
    alert(c+' '+d); // data sent
    alert (lookup(c,d)); //data returned from ajax

function lookup(c,d){
  // type of look up c (cust, cusip, curve )
  // d is data in the <td>
  method: "POST",
  url: "ajaxlookuppage.php",
  data: { type: c, data: d }
  .done(function( data ) {
    return( data);
  <meta charset="utf-8">
  <title>JS Bin</title>
  <tr><td>CUSIP </td><td>customer ID</td><td>Curve ID</td></tr>
  <tr class="get"><td class="cusip">123 </td><td class="cust">Cust1000</td><td class="curve">Curve5867</td></tr>
   <tr class="get"><td class="cusip">124 </td><td class="cust">Cust1001</td><td class="curve">Curve5869</td></tr>

Open in new window

jkurantAuthor Commented:
I have determined that I can use document.selected.createRange().text. The user could select the text they want to look up and then could click on a lookup icon to start the lookup process. I would use the onmousedown event, because the click event clears the selection first.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jkurantAuthor Commented:
No one actually found a solution for me. I found it. The selection is not wiped out during the onmousedown event, so I can use that along with selected text.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.