Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11

Thread: copy innerHTML

  1. #1
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,994
    Thanks
    120
    Thanked 76 Times in 76 Posts

    copy innerHTML

    since there are troubles with innerHTML in IE .....

    instead of :

    Code:
    target.innerHTML = src.innerHTML;
    -->

    Code:
      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 ?
    Last edited by BubikolRamios; 11-15-2010 at 07:58 AM.
    Found a flower or bug and don't know what it is ?
    agrozoo.net galery
    if you don't spot search button at once, there is search form:
    agrozoo.net galery search

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    What troubles are you experiencing with innerHTML in IE?
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #3
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,994
    Thanks
    120
    Thanked 76 Times in 76 Posts
    If I do now:
    Code:
    target.innerHTML = src.innerHTML;
    then I get, from target.innerHTML: getElementsbyTagName(..)... ----> is null or not an object, IE only. That forinstance.
    Found a flower or bug and don't know what it is ?
    agrozoo.net galery
    if you don't spot search button at once, there is search form:
    agrozoo.net galery search

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,474
    Thanks
    13
    Thanked 361 Times in 357 Posts
    Quote Originally Posted by BubikolRamios View Post
    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).
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #5
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,994
    Thanks
    120
    Thanked 76 Times in 76 Posts
    ehh, yes, that was a typo.

    Trying to reproduce problem outside my code, with no success, everything works....
    Found a flower or bug and don't know what it is ?
    agrozoo.net galery
    if you don't spot search button at once, there is search form:
    agrozoo.net galery search

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,316
    Thanks
    203
    Thanked 2,565 Times in 2,543 Posts
    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.
    Last edited by Philip M; 11-15-2010 at 02:07 PM.

  • #7
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,994
    Thanks
    120
    Thanked 76 Times in 76 Posts
    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:
    Code:
    src = document.getElementById('comment_edit_save');
    alert(src.childNodes.length);
    FF gives me 7 IE 3
    Last edited by BubikolRamios; 11-15-2010 at 10:49 PM.
    Found a flower or bug and don't know what it is ?
    agrozoo.net galery
    if you don't spot search button at once, there is search form:
    agrozoo.net galery search

  • #8
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,461
    Thanks
    11
    Thanked 600 Times in 580 Posts
    Quote Originally Posted by BubikolRamios View Post
    Code:
    src = document.getElementById('comment_edit_save');
    alert(src.childNodes.length);
    FF gives me 7 IE 3
    is that even after you run normalize() ?
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%

  • #9
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,994
    Thanks
    120
    Thanked 76 Times in 76 Posts
    ok squized things as much as I could. Can anyone tell why textarea does not alert out ?

    Test page:
    http://agrozoo.net/test.html
    Last edited by BubikolRamios; 11-16-2010 at 12:51 AM.
    Found a flower or bug and don't know what it is ?
    agrozoo.net galery
    if you don't spot search button at once, there is search form:
    agrozoo.net galery search

  • #10
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    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:

    Code:
    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.

    Code:
    var textareaMarkup = $('#comment_edit_save .comment_text_area').html();
    alert(textareaMarkup);
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #11
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,994
    Thanks
    120
    Thanked 76 Times in 76 Posts
    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.

    Code:
    <textarea id ="textarea" cols="70" rows="5"></textarea>
    or
    Code:
    <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.
    Last edited by BubikolRamios; 11-16-2010 at 04:24 PM.
    Found a flower or bug and don't know what it is ?
    agrozoo.net galery
    if you don't spot search button at once, there is search form:
    agrozoo.net galery search


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •