JavaScript IE appendChild() - Unexpected call to method or property access
Asked Answered
S

7

5

I have been having this problem in IE. I have two divs which I use to drag selected elements from one to another. Lets say I have a child element (also a div) in div1 and some child elements in div2. I call the div2.appendChild() method on child element of div1. It removes the child from div1 and appends it to div2. If I then try to append the child back to div1 I get the following exception "Unexpected call to method or property access" in IE. It is working perfectly in firefox. See below code snippet for the javascript.

function moveSelectedGroupBoxItems(toLocation, grp){
    document.body.className = 'groupBoxDefaultCursor';
    if(groupBoxfromLocation != toLocation){
        if(grp == groupBoxGroup){
            var fromVal = document.getElementById(groupBoxfromLocation);
            var toVal = document.getElementById(toLocation);

            var children = fromVal.childNodes;
            for (var i = children.length - 1; i >= 0; i--) {
                if(children[i].className == 'groupBoxItemSelected'){
                    children[i].childNodes[0].name=toLocation;
                    toVal.appendChild(children[i]);
                }
            }
        }
    }
    groupBoxfromLocation = '';
    groupBoxGroup = '';
    return false;
}

This basically moves the selected child divs from one parent div to another on dragging.

Sybarite answered 10/11, 2009 at 15:14 Comment(1)
...and these are definately div's? not option, table, thead, tfoot, tbody, tr, td, script, style or param elements? ;-)Careen
S
0

SOLVED!... sort of I had hidden inputs within the child divs which stored the id of the item being moved for form submission. I removed the hidden inputs and placed them underneath my selectionboxes. Each time an div is moved it updates the name of the hidden to the parent divs id. It seems IE has a problem with appendChild() when the node has children. This did sometimes work and sometimes it failed. Appending a single element with no children always works. I am not sure exactly what the problem was, but the above sorted it out.

Cheers

Grep

Sybarite answered 12/11, 2009 at 8:21 Comment(0)
S
5

For search results benefits - I had this same error in IE 6 and 7 "Unexpected call to method or property access" when appending a child node to a namespaced element, but the namespace hadn't been declared.

var a=document.createElement("foo:blah");
var b=document.createElement("div");
a.appendChild(b); //Fails

To get around this, I had to add the namespace declaration either into the HTML code:

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:foo>

or dynamically via javascript (Probably should be wrapped in try/catch):

var namespaces = document.namespaces;
if(namespaces) {
    namespaces.add("foo", null, null);
}
Sycamore answered 23/12, 2010 at 16:6 Comment(1)
+1 This didn't exactly solve my problem, but lead me to the solution. Mine was using the HTML5 tag <section> without an HTML5 shiv. Changed it to a <div> and it worked.Textualist
A
3

I had this issue in IE7 and it turned out to be that the IE7 DOM was not perfect.

In my case I had an image tag rendered by the ASP.NET MCV TagBuilder, which rendered it as:

<img ...></img>

Note the ending tag there and that was what caused the problem. It worked when rendered as:

<img ... />

Hope this helps.

Amelioration answered 17/9, 2010 at 7:17 Comment(0)
C
2

I agree with Kaze noe Koe, dynamically setting the name of elements is not the most bullet-proof practice. Unfortunately, I cannot tell you how to get rid of the error. However, I highly recommend using a JavaScript library (jQuery, Prototype, Dojo, ...). They are quite small, relatively easy to learn, and much more convenient to use than the horrible DOM API. Last but not least, they shield you from many such awkward Browser incompatibilities. Give it a try, after a couple of days you cannot imagine going back, I promise.

Castano answered 10/11, 2009 at 15:27 Comment(0)
B
0

Are you sure it isn't dying on:

children[i].childNodes[0].name=toLocation;

What are you trying to accomplish with this line?


Seriously, try to comment it out. I don't even think it is what you think it is - isn't the right property called nodeName?

Bolshevik answered 10/11, 2009 at 15:23 Comment(0)
S
0

children[i].childNodes[0].name=toLocation;

Each child div contained in the parent div also contains a hidden input value. The parent divs do not contain a name attribute, only an id. When the child divs are dragged from one parent div to another parent it updates the name value of all the hidden inputs to the divs id. When the form is submitted the server can then distinguish which values were present in the specific parent div. This works in IE and Firefox without fail thus far. The problem definately lies within the appendChild() method as it keeps breaking on this statement. If a parent div is empty at load everything works perfectly. If a parent divs children are appended to a different div and then back again I get the exception in IE only.

Unfortunately I cannot implement a framework as of yet. Our tags frequently change to accomodate new requirements.

Thanks for your replies on my topic, hope this makes more sense now.

Cheers

Grep

Sybarite answered 11/11, 2009 at 7:35 Comment(0)
S
0

SOLVED!... sort of I had hidden inputs within the child divs which stored the id of the item being moved for form submission. I removed the hidden inputs and placed them underneath my selectionboxes. Each time an div is moved it updates the name of the hidden to the parent divs id. It seems IE has a problem with appendChild() when the node has children. This did sometimes work and sometimes it failed. Appending a single element with no children always works. I am not sure exactly what the problem was, but the above sorted it out.

Cheers

Grep

Sybarite answered 12/11, 2009 at 8:21 Comment(0)
N
0

just had the same problem with IE7, cured it by adding a timeout:

setTimeout(function(){gallery.appendChild(g_field)},300);

I've lost count of the no of times something doesn't work in IE but works perfectly in Firefox that can be solved by a short delay...no idea why!

Sorry! spoke to soon...turns out my timeout (in this case) didn't work.

Nettie answered 5/1, 2011 at 6:16 Comment(1)
The most confusing part of all of this I bet if you inserted an alert("") before gallery.appendChild(g_field) without the delay it would probably work. IE the only browser where an alert has additional functionality, other than displaying a message.Sybarite

© 2022 - 2024 — McMap. All rights reserved.