I try put one element next second, and delete second element, but it does not work.
Look at example: http://jsfiddle.net/CZCGG/
I try put one element next second, and delete second element, but it does
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The result of
getElementsByTagNameis a live node list. As you manipulate the elements the pseudo-array you have will get updated in real-time.Specifically each time around the loop your
ivalue is jumping too far ahead, because the element after the one you just removed now has that original element’s index.To resolve that you should work from the end of the array backwards. This ensures that each element retains its original index as the loop progresses.
You should also use
Node.replaceChildinstead of aninsertBefore,removeChildsequencesee http://jsfiddle.net/alnitak/pfnGX/