...

View Full Version : copy innerHTML



BubikolRamios
11-15-2010, 06:15 AM
since there are troubles with innerHTML in IE .....

instead of :


target.innerHTML = src.innerHTML;

-->



this.copy_innerHTML = function(src,target)
{
//remove all target inner elements
while(target.hasChildNodes()){target.removeChild(target.childNodes[0])}

for (var j = 0; j<src.childNodes.length-1; j++)
{
target.appendChild(src.childNodes[j].cloneNode(true));
}

}




a) Tested in FF and GC

b) in IE this works, but stuff does not appear where it should.

EDIT: if text nodes removed , then even in ff does not work, does not find textarea inside target node.
That is, the error pops up, previously only notable at IE. HTML & CSS validation is OK.

Any advice ?

glenngv
11-15-2010, 09:53 AM
What troubles are you experiencing with innerHTML in IE?

BubikolRamios
11-15-2010, 10:04 AM
If I do now:


target.innerHTML = src.innerHTML;


then I get, from target.innerHTML: getElementsbyTagName(..)... ----> is null or not an object, IE only. That forinstance.

Dormilich
11-15-2010, 11:51 AM
then I get, from target.innerHTML: getElementsbyTagName(..)... ----> is null or not an object, IE only. That forinstance.
itís getElementsByTagName() not getElementsbyTagName() (JavaScript as case-sensitive).

BubikolRamios
11-15-2010, 12:09 PM
ehh, yes, that was a typo.

Trying to reproduce problem outside my code, with no success, everything works....

Philip M
11-15-2010, 01:04 PM
Textarea elements can only contain text and the way of accessing or modifying that data is via the value property, not innerHTML.


All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

BubikolRamios
11-15-2010, 09:38 PM
First conclusion: when you do x.innerHTML = y.innerHTML
then IE does not see anything inside x.innerHTML via dom, like, insertbefore ... does not work.

second:


src = document.getElementById('comment_edit_save');
alert(src.childNodes.length);

FF gives me 7 IE 3

rnd me
11-15-2010, 10:13 PM
src = document.getElementById('comment_edit_save');
alert(src.childNodes.length);

FF gives me 7 IE 3

is that even after you run normalize() ?

BubikolRamios
11-15-2010, 10:42 PM
ok squized things as much as I could. Can anyone tell why textarea does not alert out ?

Test page:
http://agrozoo.net/test.html

glenngv
11-15-2010, 11:55 PM
Childnodes in FF include whitespace that's why you get more childnodes in FF than IE.

If you know that the textarea's parent div is the 2nd div of comment_edit_save div, then just do it simply like this:


var textareaMarkup = document.getElementById('comment_edit_save').getElementsByTagName('div')[1].innerHTML;
alert(textareaMarkup);



It would be much much easier and less error prone if you use jQuery.


var textareaMarkup = $('#comment_edit_save .comment_text_area').html();
alert(textareaMarkup);

BubikolRamios
11-16-2010, 07:25 AM
The point is:
1. text area comes in package of bunch of child nodes that needs to be copyed to some other place. Source package beeing changed, textarea changing place, ...
2.Copying that package works fine with innerHTML, but then in IE, it cant be inserted anything into that resulting innerHTML via DOM.
3. So I need to copy stuff via childNodes.cloneNode(true);

And that thing for some reason sometimes does not see textarea, I mean the cure could be just this difference.



<textarea id ="textarea" cols="70" rows="5"></textarea>

or


<textarea id ="textarea" cols="70" rows="5">
</textarea>

and then childNodes sees texarea(same in FF and IE), but can't guarantee that, seems to be pretty randome, when it see it and when not, depends on surrounding HTML.

So manualy finding textarea with getelemantsbytagname('textarea')[x, x beeing actualy x] wouldn't be good/possible coz ocasionaly looping thrue child nodes would allready find that textarea. And I allways on the end compress HTML, so looping thrue child nodes will newer find textarea.

I'm a bit sceptic that I would be the first that digged out this textarea prob.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum