Skip to content
This repository

NOT_FOUND_ERR: DOM Exception 8 InsertItemBefore #476

Open
kmklapak opened this Issue · 10 comments

3 participants

kmklapak Tim Neil Lawrence Liu
kmklapak

function insertErrorTweet(errorThrown) {

var list = document.getElementById('thelist');
var items = list.getItems();

var date = new Date();
var item = document.createElement('div');

for (i = 0; i < items.length; i++) {
    if (items[i].getAttribute("type") == "errorTweet" && items[i].getAttribute("error") == errorThrown) {
        $(items[i]).remove();
        return;
    }
}

item.setAttribute('data-bb-type', 'item');
item.setAttribute('data-bb-img', "images/error.png");
item.setAttribute('data-bb-accent-text', relativeTime(date));
item.setAttribute('data-bb-title', "An Error As Occured");
item.innerHTML = errorThrown;
item.setAttribute('type', "errorTweet");
item.setAttribute('error', errorThrown);


var newlist = document.getElementById('thelist');
var newitems = newlist.getItems();

list.insertItemBefore(item, newitems[0]);

$('#pullDownText').removeClass("pullDownLoader");
$("#pullDownImage").show();
$("#loadingWidget").hide();

myScroll.refresh();

}

The above function removes any existing error items from the image list, and then adds the new error to the list. This seems to only work if nothing is removed while iterating the for loop. Otherwise I get an NOT_FOUND_ERR: DOM Exception 8 when trying toadd the new item using insertitembefore.

Any ideas? Im assuming the DOM isnt being updated?

Tim Neil
Owner

It looks like you are removing all the items from the list, and then inserting a new item using insertItemBefore(item, items[0]); But you're items array is not getting cleared.

In the loop above when you do a items[i].remove(), it will remove the item from the DOM.. which means it doesn't exist anymore. so when you try and do an insertItemBefore you are passing in an undefined object at index[0] because the DOM element it contained before no longer exists.

kmklapak

Yup I understand that. I've also tried creating a new array before using insertItemBefore and it was giving me the same error:

var newList = document.getElementById('thelist');
list.insertItemBefore(item, newList[0]);

Tim Neil
Owner

Yes, that is expected.. the second parameter in the insertItemBefore() needs to be an already existing item in the list. It's purpose is if you need to insert an item at a specific location. If you are looking to sequentially add items to a list then you will want to use appendItem(item)

kmklapak

I'm confused now. I only ever remove the first item in the list and I still have multiple items in the DOM that I can confirm. Lets say I have items 1,2,3,4 and essentially I want to replace item 1. Why can't I remove that item, then use document.getElementById('thelist') to get the new array of size 3? Its as if the items array items[0] is still pointed to item 1 when it should now be item 2. Does that make sense?

Tim Neil
Owner

That does make sense, and it should all work. I have an example in the add/remove buttons that shows doing basically what you're talking about.

Without tracing through your code it will be hard to determine what may be going wrong.

kmklapak

I've updated the code above to what I think should work and I also think I know whats happening. When I remove the item, it removes it from the DOM, but for some reason when I do newitems = newlist.getItems() before adding the new item I get the original list of items including the one that I supposedly removed from the DOM. So in my case I have 21 items, I remove the top 1 , and I when I try to get my updated array of items, it is still 21 items not 20 like I would expect, hence the NOT_FOUND_ERR.

Tim Neil
Owner

Hmmm.. interesting. You were seeing this on BB10 correct?

kmklapak

I spent a bunch of time testing today and it seems like the issue is Ripple. The code works on any device, BB10 or Playbook, but its in Ripple when I see the issue.

Tim Neil
Owner

Well that's interesting.. I'll have to do some debugging to see why it isn't working in Ripple

Lawrence Liu
xqliu commented

This issue also happens for me,

metaList.insertItemBefore(item, metaList.getItems()[0]);
metaList.insertItemBefore(item, metaList.childNodes[0]);

The above two line of code, the first line always throw an NOT_FOUND_ERR: DOM Exception 8 exception, but the second works fine for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.