Tech Support Guy banner
  • Please post in our Community Feedback thread for help with the new forum software! If you are having trouble logging in, please Contact Us for assistance.
Status
Not open for further replies.
1 - 1 of 1 Posts

·
Registered
Joined
·
6,052 Posts
Discussion Starter · #1 ·
i'm playing around with a script that creates a black box when you click a link, then deletes them all when you click another. It makes them just fine, but for some reason its screwing up on the deletion.

Code:
function open_new(){
var divs = document.getElementsByTagName('div');
var a = 1;
for (i = 0; i < divs.length; i++) {
	if(divs[i].getAttribute('rel') == 'win') {
	a++;
	}
	}
	
var bod = document.getElementsByTagName('body')[0];
var overlay = document.createElement('div');
overlay.setAttribute('style', 'width:100px; height:100px; background-color:#000000;');
overlay.id = a;
overlay.setAttribute('rel', 'win');
bod.appendChild(overlay);
var text = document.createTextNode(overlay.getAttribute('id'));
overlay.appendChild(text);
}

function close_all(){
var wins = document.getElementsByTagName('div');
var bod = document.getElementsByTagName('body')[0];
for (i = 0; i < wins.length; i++) {
	if(wins[i].getAttribute('rel') == 'win') {
	bod.removeChild(wins[i]);
	}
	}
}
i know for a fact that the array created by var wins is the right amount every time, the problem is that the for loop isn't taking it correctly. if, for example, i make five new boxes then wipe them, it only deletes boxes 1, 3, and 5. If i make four of them, it deletes 1 and 3. It's only deleting the oddly id'd boxes. I've attempted putting a marker before that last if statement, so that every item the loop goes through will give an alert. If i make five boxes, it gives a marker for only 4 items. I don't know why, but the loop is skipping evenly id'd boxes, excluding 0. Once it only has evens, it deletes only every other one of those(if only 2, 4, 6, 8 are left it gets rid of 2 and 6).

I am baffled.

in action can be seen here
 
1 - 1 of 1 Posts
Status
Not open for further replies.
Top