Solved

How to capture select range event on a contenteditable div?

Posted on 2003-11-06
6
2,781 Views
Last Modified: 2007-12-19
Hi Experts,

Say i have:

<div id="myDiv" ContentEditable="true">blablabla blablabla</div>

Which event is triggered when i select (highlight) some of the text inside that div, and how do i catch it?

What i'm looking for is an equivalent to the "DisplayChanged" event fired by the old DHTML Editing Component.

Thanks in advance :)
0
Comment
Question by:psogaa
  • 3
  • 3
6 Comments
 
LVL 25

Expert Comment

by:devic
ID: 9694179
hi psogaa,

so:
===========================================================
<div id="myDiv" ContentEditable="true">blablabla blablabla</div>

<button onclick=alert(document.selection.createRange().text)>getSelection()</button>
0
 
LVL 2

Author Comment

by:psogaa
ID: 9694258
That is not what i want.

I want to capture the event that a user selects some text in my div.

From MSDN:
***************************************************************************
How do I update my application's menus and toolbars based on the current selection?
The DHTML Editing control fires the DisplayChanged event whenever the editing context has changed (for example, when the user has selected an element). Use this event to update your menus and toolbars by calling QueryStatus. The VBEdit sample in the DHTML Editing Component SDK demonstrates this technique by updating its toolbar to reflect formatting of the current selection.
***************************************************************************

This is exactly what I want to do - only I don't use the DHTML Editing control, i use a ContentEditable Div instead.
0
 
LVL 25

Expert Comment

by:devic
ID: 9694448
try this:

<div id="myDiv" onkeyup=runit() onmouseup=runit() ContentEditable="true">blablabla blablabla</div>
<script>
function runit()
{
      alert(document.selection.createRange().text);
}
</script>
0
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
LVL 25

Accepted Solution

by:
devic earned 500 total points
ID: 9694466
or better so:
==================
<div id="myDiv" onkeyup=runit() onmouseup=runit() ContentEditable="true">blablabla blablabla</div>
<script>
function runit()
{
      if(document.selection.createRange().text!="")
      {
            alert(document.selection.createRange().text);
      }
}
</script>
0
 
LVL 2

Author Comment

by:psogaa
ID: 9694519
Hmm, yeah i guess keyup and mouseup covers all possible ways of selecting content.
Was hoping for a specific event for context change instead of a workaround.

I'll leave the question open for a while longer before i accept your comment as answer if you don't mind.

Thanks for the help so far :)
0
 
LVL 2

Author Comment

by:psogaa
ID: 9694696
Actually it works like a charm with those 2 events :)

Thanks!
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
What does GoogleTagMgr javascripts below do 5 36
Javascript/JQuery - HtmlEncode (?) Ajax Result 1 67
Html Newline 7 21
Html input text color the frame line 2 22
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question