JavaScript
Article
By Sam Deering

jQuery Highlight Text

By Sam Deering

Simple jQuery and CSS code snippets to highlight text on page in order to select it for copy or it could be used in combination with a search script to show where matches are found on a page.


Note: CSS3-only feature (only Firefox and Safari too as far as I know).

::selection {
    background: #ffb7b7; /* Safari */
}
::-moz-selection {
    background: #ffb7b7; /* Firefox */
}

Or use a textbox:

--ADVERTISEMENT--


function selectAllText(textbox) {
    textbox.focus();
    textbox.select();
}
jQuery('#txtInput').click(function() { selectAllText(jQuery(this)) });
Recommended
Sponsors
The most important and interesting stories in tech. Straight to your inbox, daily. Get Versioning.
Login or Create Account to Comment
Login Create Account