Auto highlight text inside pre tags using jQuery

We use pre tags in the Polldaddy plugin to display the embed codes and short URLs. We use pre tags because it formats the text inside the tags as we insert it, which is ideal for displaying code in particular. The thing is, people like to copy this code and they have to go to the awful bother of selecting all the text manually if they wish to copy the code or URL.

The alternative is to use an text input or text area which will allow you to use a select method that you can call on a click event, but then you lose the neat formatting that pre tags offer.

So I had a look for a jQuery hack to auto select all the text inside the pre tags. The only complication with this appears to be a cross platform solution as each browser appears to have their own way of selecting text.
Continue reading “Auto highlight text inside pre tags using jQuery”