selecting all text within a div on a single left click with javascript

Posted by tstyle on Stack Overflow See other posts from Stack Overflow or by tstyle
Published on 2011-01-02T12:40:17Z Indexed on 2011/01/03 0:53 UTC
Read the original article Hit count: 138

Filed under:
|
|
|

I have a simple non-clickable link within a div that looks like this: alt text

It's meant to be a sharable link that the user can copy paste into other things.

For usability purposes, I want a single left click anywhere within the div to select the entire link: alt text

I don't know much about, javascript/web programming, so I've tried the following:

<div id="share_link" onClick="select_all('share_link')"><%= request.url %></div>

and this javascript

<script type="text/javascript">
    function select_all(id) {
        document.getElementById(id).focus();
    }
</script>

This doesn't work. I'd like to know what's the simplest thing I should do to achieve what I want. I thought about changing the div to a text input or the text within to be a link, but ideally the content within should be read-only, non-editable, and non-clickable

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery