Solved

How to capture select range event on a contenteditable div?

Posted on 2003-11-06
6
2,699 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now