473,405 Members | 2,445 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

How to get the selected image inside a contentEditable div

Hello,
I wrote a little rich text editor. Like many widgets of this type, it uses an iframe that contains a div element with contentEditable set to true. It works pretty well except for one thing: I would like users to be able to select an image and then click a button to edit the image's HTML attributes. I've waded through pages and pages of Google results but have not found a solution that works in W3C compliant browsers (although I did find some results that explain how to do this in Internet Explorer). I am able to get a text selection without any problem; but the same methods that work for text do not work for an image. I have tried the following:

Expand|Select|Wrap|Line Numbers
  1. iframe.contentWindow.getSelection();// returns an empty string.
  2.  
  3. iframe.contentWindow.getSelection().getRangeAt(0);// also returns an empty string
  4.  
  5. iframe.contentWindow.getRangeAt(0);// is not a function.
  6.  
  7. document.activeElement;// returns the object HTMLButtonElement
  8.  
  9. iframe.contentDocument.activeElement;// returns the object HTMLDivElement - the image's parent node.
  10.  
  11. iframe.contentWindow.activeElement;// is undefined.
  12.  
  13. iframe.contentDocument.selection;// is undefined.
  14.  
  15. iframe.contentWindow.selection;// is undefined.
  16.  
As you can see I was kind of reaching by the end there. I'm all out of ideas now, so if anybody has tips or pointers I would be glad to hear them.

Thanks very much!
Oct 7 '10 #1
3 6909
acoder
16,027 Expert Mod 8TB
Perhaps you can use activeElement and use it to get at the image child (if you keep each image separate, so you know that if a parent element is selected, then it must be the image contained within which is selected).

Alternatively, you could link each image (this may not be appropriate depending on your requirements).
Oct 8 '10 #2
Hi acoder,
Thanks very much for your reply. I appreciate the thought; and you are correct that if there were only one image within my contentEditable div, then I could simply navigate down to the image from there. Unfortunately I have no way of knowing how many images will be contained inside the div, so I can't reliably predict that a particular image child node is necessarily the selected image.

What I wanted was a button that would add a class to any selected element, whether it is an image or a paragraph or whatever. I don't understand why getSelection returns a value when I have selected text, but returns an empty string when I have selected an image. A browser bug? (I also tried getSelection.parentNode, hoping that I could programmatically wrap all images in a p tag and then apply classes to that tag... no dice; it returns undefined.)

Sadly, I'm coming to the conclusion that this is not possible. I've already started on a workaround alternative.

I was glad to get some feedback though. Thanks again for your reply!
Oct 8 '10 #3
lbried
1
Stupid solution...but it works ! (tested both on IE and Firefox)

Expand|Select|Wrap|Line Numbers
  1. contentWindow.document.execCommand('CreateLink',false, 'http://imageselectionhack/');
  2.   allLinks =   contentWindow.document.getElementsByTagName('A');
  3.   for(i = 0; i < allLinks.length; i++)
  4.    {
  5.      if (allLinks[i].href == 'http://imageselectionhack/')
  6.       {
  7.         curObj = allLinks[i].firstChild;
  8.         contentWindow.document.execCommand('Undo'); // undo link
  9.         break;
  10.       }
  11.    }
  12.  
  13. if ((curObj) && (curObj.tagName=='IMG'))
  14. {
  15.   //do what you want to...
  16. }
Jan 3 '12 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Abs | last post by:
Hi! I need to put an image inside a box, centering and resizing it to fit the box. The box is positioned and sized in the page using percent values. The problem is that I don't know how can I do...
2
by: TnaG | last post by:
I have an onClick on a TD. If I click on an image inside the TD, the onClick fires, however if i click anywhere else inside the TD nothing happens. Any ideas?
3
by: coolsti | last post by:
I need some help here. I am making an application which allows a user to look at a series of picture files one at a time, and enter the outcome of various visual tests to a database. The...
2
by: Francisco | last post by:
Hello, I'm creating my WYSIWYG html web editor. but now I want to know witch image is selected. I try the selection.createRange().htmlText but it returns me null. I see that when i have an image...
10
by: Raghavendra RAV | last post by:
Hi, I need to store an image from a Graphics object(.NET) inside a xml data island. Anyone might have came accross or have an idea how to do this. Please share. Thanks & Regards, Raghu, CSS...
3
by: Sachin | last post by:
If I have an image inside the RichTextBox, can I drag/move it inside the RichTextBox with the help of mouse, just like we can move any control on the form during design time. Please guide -- sachin
1
by: Chris Leffer | last post by:
Hi. I have an image inside a repeater that needs an onclick event, dynamically generated. I have this code on my .aspx: <IMG onclick='<%# "toggleDetails('" &...
1
by: matthud | last post by:
I am using window.content.getSelection() to get selected text, but how can I get the selected image or at least the element of the highlighted image?
3
by: Brandon Arnold | last post by:
I'm having an issue that I can't quite figure out. I have a PictureBox that initially sets a picture from an embedded resource. The user clicks a listview item and if an image url is available, it...
0
by: Meelis Lilbok | last post by:
Hi I have a following question Lets say we have a Panel and inside that panel a Image control on ASP.NET web site. Panel siz is 200x200px Image size is 400'x400
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.