Solved

How to capture select range event on a contenteditable div?

Posted on 2003-11-06
6
2,913 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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

617 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