473,714 Members | 2,623 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

IE: Inserting a dom element into existing text

3 New Member
Hi.

I need to insert an image element into text in an iframe in design mode. That part I can do:

Expand|Select|Wrap|Line Numbers
  1. var sel = oEditor.eFrameDoc.selection;
  2. var range =    sel.createRange();
  3. range.pasteHTML('<img src="'+imagePath+'" border="0" />');
But I need a reference to that new IMG tag to do other stuff with it.

In Gecko, I can insert a node dynamically, as one would expect, thus:

Expand|Select|Wrap|Line Numbers
  1. var sel = oEditor.eFrameWindow.getSelection();
  2. var range = sel.getRangeAt(0);
  3. var img = document.createElement('img');
  4. img.setAttribute('src', imagePath);
  5. img.setAttribute('border', 0);
  6. range.insertNode(img);
  7.  
In Gecko, I don't need go looking for my new image in the DOM tree since I created it myself already -- img is the object I need. I can work it after inserting it, no problem.

IE's TextRange is limited -- you can't insert DOM elements directly. So I guess what I need is a way to maybe split an IE TextArea either side of a new "empty element" (whatever that might be), then use emptyObject.ins ertBefore(img) -- but I'm only guessing.

I've Googled this to death over several hours to no avail (except to find this site! :) Any assistance would be greatly appreciated.

Gruvin.
May 5 '07 #1
2 3446
pronerd
392 Recognized Expert Contributor
I need to insert an image element into text in an iframe in design mode. That part I can do:
The lazy option would be to add an ID attribute to the image tag.
[HTML]<img src="'+imagePat h+'" id="imageTagICr eated" border="0" />[/HTML]

Then you could find it with something like :
Expand|Select|Wrap|Line Numbers
  1. var imageTag = document.getElementById('imageTagICreated'); 

The other option would be to use the insertBefore() or appendChild() methods. These will let you add your created element as a child to other elements. They are supported in both browsers.

http://onlinetools.org/articles/unobtrusivejava script/chapter3.html
May 6 '07 #2
gruvin
3 New Member
The lazy option would be to add an ID attribute to the image tag.
Yes -- I forgot to mention I have (good :) ) reasons not to do that.

The other option would be to use the insertBefore() or appendChild() methods. These will let you add your created element as a child to other elements. They are supported in both browsers.
Ah yes -- except that IE does not support those methods (see my original message) for a TextRange, being as the good folks at M$ evidently decided a range a) must be either a Text or Control thing (can't contain or will at least ignore elements) and b) therefore will only allow the changing of texty type stuff. Yeh! (not)

The most logical thing I would have though could work with insertBerfore would be range(0).parent Node. But no, that would be just too obvious and simple, apparently. :P

Thanks for your help -- I'm still looking for a way to do this. Meanwhile, I've mangled my code left and right to approach the entire (parent) problem differently, just for IE. *sigh*

P.S: This all in IE 6 -- I haven't tried in IE7. Many people are still using 6 anyway.
May 7 '07 #3

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

Similar topics

1
2276
by: CB | last post by:
I have a group of documents that previously had no schema, now I have a schema, and I want to use XSLT to fixup the documents by inserting the namespace reference. In other words, my input document looks like <SiteAdaptationFile> ... </SiteAdaptationFile>
2
7985
by: Wim Roffal | last post by:
I want to insert an option into a select. I know you can do something like: - Myselect.options = new Option('Hello 3'); but that doesn't do what I want because it overwrites an existing option. I have the existing options ordered on alphabet and the new option should fit in on the right place. Of course I can move all the options that come after the new one, but I wondered if there is a smarter way to do this.
1
1402
by: Stanimir Stamenkov | last post by:
I've wondered if it is right thing to do: element.parentNode.insertBefore(element, beforeElement); where 'beforeElement' is one of the 'element.parentNode.childNodes'? First, I've used: var parentElement = element.parentNode; parentElement.removeChild(element);
2
2917
by: Asad | last post by:
I have a form on a page that has several textareas, and textboxes inside a table (so the table containing the textboxes is also inside the FORM tag). I want to replace the textareas with simple text instead. But I want to keep the format of my page EXACTLY the same. However, the problem is that ... 1) Javascript won't let me create say a one-cell TABLE containing some text (e.g. textarea's value) and then insertBefore an element in the
1
1343
by: Stephen | last post by:
I have the following for a datagrid that inserts a row into an xml file. It works great, but inserts the data to a new top-level node. I don't know how to specifiy an existing node for the inserted item: void DataGrid1_Update(Object Sender, DataGridCommandEventArgs e) { try { string stitle = (e.Item.Cells.FindControl("txttitle") as TextBox).Text; DataSet ds = new DataSet();
0
1328
by: ruediger | last post by:
Hi there, I want to set up an XML Schema for documents of the following structure: The <root> node contains - an optional <message> element with a text attribute, and - arbitrary further content. To make the parts unique, the top-level element of the further content
2
4961
by: C# to XML | last post by:
Hi friends, I am new to csharp and XML : fp=File.OpenText(server.mappath(".\\upload\\")+"test.txt"); string info=fp.readtoend(); string arinfo =new string; char splitter = {'|','^'}; arinfo =info.split(splitter);
2
1595
by: petermichaux | last post by:
Hi, My AJAX response is a bit of HTML that I insert into the DOM. I extract the contents of the script elements in the response into an array called aScripts. I then want to append these to an element so that the scripts will actually execute. I am trying a variation on what Randy Webb posted a few months ago. function insertScripts(element, aScripts) { if(typeof element == "string") {element =
4
2582
by: Anand | last post by:
Hi, I'm new to Python. we are using python2.4. I wanted to insert an element into an existing xml file. Can anyone help me with this? I've seen lxml and elementTree in python2.5 has some API's to insert into existing xml file. We cant use python 2.5 due to some limitations. So how can we do it in 2.4?
0
9314
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9174
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9074
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9015
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6634
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5947
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4725
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3158
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2110
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.