JQuery: Selecting Text in an Element (akin to highlighting with your mouse)

Posted by Jason on Stack Overflow See other posts from Stack Overflow or by Jason
Published on 2009-06-12T06:35:12Z Indexed on 2010/04/07 16:23 UTC
Read the original article Hit count: 144

Filed under:
|
|

I would like to have users click a link that then selects the html text in another element (NOT an input). By "select" I mean the same way you would select text by dragging your mouse over it. This has been a bear to research because everyone talks about "select" or "highlight" in other terms.

Is this possible?

My code so far is thus:

html:

<a href="javascript:" onclick="SelectText('xhtml-code')">Select Code</a>
<code id="xhtml-code">Some Code here </code>

js (jquery):

function SelectText(element) {
    $("#" + element).select();
}

but it doesn't work. Am I missing something blatantly obvious?

Thanks

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about select