Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to capture select range event on a contenteditable div?

Posted on 2003-11-06
6
Medium Priority
?
3,038 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 25

Accepted Solution

by:
devic earned 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…
Suggested Courses

782 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