# [web] Javascript: removing consecutive elements

This topic is 3850 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey, I'm writing a GreaseMonkey script to clean up some HTML and it goes fairly well. I can remove any elements I don't need and mark some important ones. The problem I have is removing multiple consecutive BR tags, since the removing of elements leaves them behind. If I use something like:
var brs = document.evaluate("//br", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
then the nextSibling property is always null so I cannot check if two BR's follow eachother. Does anybody have an idea on how to go about this? tia, CipherCraft

##### Share on other sites
Im not too sure of what GreaseMoney is but I do see a potential issue with what you are describing.

If you remove the BR and then try to access its nextSibling, then I would expect it to be null because it (the BR you just removed) no longer exists, which I also expect is what is happening in your case.

You would need to either store BR's next sibling or delete from the bottom up using recursion:

func remove(curObj)
{
if(curObj.hasSibling)
remove(curObj.sibling);
end if

delete curObj;
}

Using the above pseudo, you would need to check if each object is of type "BR" accordingly. Your BR's will then be removed starting with the last one in the consecutive sequence of BR's.

##### Share on other sites
Quote:
 Original post by ju2wheelsIm not too sure of what GreaseMoney is but I do see a potential issue with what you are describing.

Clicky

Quote:
 Original post by ju2wheelsIf you remove the BR and then try to access its nextSibling, then I would expect it to be null because it (the BR you just removed) no longer exists, which I also expect is what is happening in your case.

I would expect that I'd thought of that [wink] but I'll check tomorrow at work to see if that's my oversight...

Quote:
 Original post by ju2wheelsYou would need to either store BR's next sibling or delete from the bottom up using recursion:func remove(curObj){ if(curObj.hasSibling) remove(curObj.sibling); end if delete curObj;}Using the above pseudo, you would need to check if each object is of type "BR" accordingly. Your BR's will then be removed starting with the last one in the consecutive sequence of BR's.

This would make sense if the above problem was the cause, yes. Again, I'll check to see if my bug wasn't hiding in plain sight.

Thanks so far,
CipherCraft

##### Share on other sites
Quote:
Original post by CipherCraft
Quote:
 Original post by ju2wheelsIf you remove the BR and then try to access its nextSibling, then I would expect it to be null because it (the BR you just removed) no longer exists, which I also expect is what is happening in your case.

I would expect that I'd thought of that [wink] but I'll check tomorrow at work to see if that's my oversight...

Looks like I'm only removing BRs from their parent if their nextSibling is also a BR; thus reducing a stretch of BR's into just one.

var brs = document.evaluate("//br", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);for (var i = 0; i < brs.snapshotLength; i++){	var br = brs.snapshotItem(i);	if (br.nextSibling != null && br.nextSibling.nodeName == "br")	{		br.parentNode.removeChild(br);	}}

But maybe the 'snapshot' request removes all sibling refs? Would be weird since the parent links are still in place...

javascript is not my full time job as you might have gathered, so any help is greatly appreciated. [wink]

tia,
CipherCraft

##### Share on other sites
1. Did you try traversing the snapshot backwards? Im not sure how the internal of all this works and how references are kept, but if you remove from front->back you may invalidate the position of everything after the first one you remove where as removing back->front may not invalidate its preceding siblings in your result set (at least not the ones you care about removing).

2. Are the parameters for "//br" and "br" case sensitive? For example, does "//BR" and "BR" produce a different result?

3. Also, Im not sure you if saw it in the Wiki you linked me to, but they prefer to copy the snapshot into a separate array for doing work on, it doesnt say why however... link

Ive never used this but seeing how the documentation describes this as a tree im guessing something might be going wrong with node referencing after a remove is done.

Hope that helps point you in the right direction.

1. 1
2. 2
3. 3
Rutin
18
4. 4
JoeJ
14
5. 5

• 14
• 10
• 23
• 9
• 33
• ### Forum Statistics

• Total Topics
632633
• Total Posts
3007543
• ### Who's Online (See full list)

There are no registered users currently online

×