views:

1742

answers:

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.

A: 

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?

Kaze no Koe
+2  A: 

Hello Grep,

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.

Cheers

Tom

Tom Bartel
I second your advice
Kaze no Koe
A: 

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

Grep
A: 

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

Grep
A: 

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.

MikeEast