/    Sign up×
Community /Pin to ProfileBookmark

Highlight Text in div

I have a code snippet in a div with scrolling overflow.

When the user clicks on the text, I want it to highlight it all for them.

Is this possible? Couldn’t find anything apart from highlighting input text.

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@boskoMay 02.2007 — Maybe this:
[CODE]<html>
<head>
<script type="text/javascript">
function highl() {
var x = document.getElementById('mytext').innerHTML;
var highlight = "<span style='background: yellow;'>"+x+"</span>";
document.getElementById('mytext').innerHTML = highlight;
}
</script>
</head>
<body>
<div id="mytext" onclick="highl();">text,text,text,text,</div>
</body>
</html>[/CODE]
Copy linkTweet thisAlerts:
@MigauthorMay 02.2007 — No sorry, I meant highlight as in select - i.e. so the user can then copy it to their clipboard.
Copy linkTweet thisAlerts:
@boskoMay 02.2007 — For div I think it's not possible but if the text is in textarea than it could be done.
Copy linkTweet thisAlerts:
@ricpMay 02.2007 — Er, yes, of course it's possible. Look into the textRange and selection objects in IE and moz respectively.

If I get some time I'll paste an example.
Copy linkTweet thisAlerts:
@MigauthorMay 02.2007 — An example would be helpful. I see a getSelection() method but nothing like setSelection().
Copy linkTweet thisAlerts:
@ricpMay 02.2007 — An example would be helpful, but as I am at work and I am only checking the site in between meetings, you'll need to wait for it.. ?
Copy linkTweet thisAlerts:
@MigauthorMay 02.2007 — Yeah don't worry, I know the deal ?

I do the same.
Copy linkTweet thisAlerts:
@MigauthorMay 09.2007 — Bump

Anyone got an example of how to do this?
×

Success!

Help @Mig spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...