Emwat Oon Posted January 7, 2014 Share Posted January 7, 2014 I'm learning javascript and I'm not sure if I follow...I'm trying to replace all instances of a class with another class.http://jsfiddle.net/UW6R2/ Link to comment Share on other sites More sharing options...
justsomeguy Posted January 7, 2014 Share Posted January 7, 2014 It gets all elements, but the list that it returns is a "live" list. That means that if you change the class of an element, it will get removed from that list. So you're skipping every other element. You change the first one, and then it gets removed, and the second one becomes the first one and the third one becomes the second one. Then when you try to change the second item in the list you're changing the third element that was originally returned.You can either loop backwards through the array, or use a while loop instead of a for loop. for (var i = div.length - 1; i >= 0 ; i--) {orwhile ((i = div.length - 1) >= 0) { 1 Link to comment Share on other sites More sharing options...
Emwat Oon Posted January 7, 2014 Author Share Posted January 7, 2014 I see; that While loop did the trick. Thanks. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now