Costanza Posted October 18, 2011 Share Posted October 18, 2011 I have some code that was working fine until I added one extra line to one of my functions. The added line: currentOptions = removeIncompatibleOptions(currentOptions); It keeps giving me this error when I call the function that has that line in it: Result of expression 'currentOptions.splice' [undefined] is not a function. Is currentOptions not actually an array? That's the only reason I can think of for why it would say that splice() was undefined. It is declared globally like this:var currentOptions = new Array(); This is the removeIncompatibleOptions() function that has splice() in it: function removeIncompatibleOptions(currentOptions){if (currentCategory == 'handguard'){var compType;if ($('barrel').hasClassName('comp228')){compType = 'comp228';}else if ($('barrel').hasClassName('comp318')){compType = 'comp318';}else if ($('barrel').hasClassName('comp400')){compType = 'comp400';}else if ($('barrel').hasClassName('comp480')){compType = 'comp480';}for (var i = 0; i < currentOptions.length; i++){if (!currentOptions[i].hasClassName(compType)){currentOptions.splice(i, 1);}}}} Link to comment Share on other sites More sharing options...
ShadowMage Posted October 18, 2011 Share Posted October 18, 2011 I recall from your previous postings that you are assigning a value to currentOptions, but I don't remember what. Post the code that assigns the values to currentOptions. Also, what line of your code does the error point to? Link to comment Share on other sites More sharing options...
thescientist Posted October 18, 2011 Share Posted October 18, 2011 the problem might be looping through the array and removing one from it each time, which might leave you with an empty array that you can't splice from anymore. First thing it to learn to debug properly. Verify the value of currentOptions as it's being passed into the function. Then it could never hurt to log for each iteration. console.log(currentOptions);for(var i = 0; i < currentOptions.length; i < l){ console.log('i is => ' + i); console.log(currentOptions); //conditional code if(){ ... } console.log(currentOptions);}; figure out the state of currentOptions at the start and end of each iteration so you can know for sure what exactly is going on. Link to comment Share on other sites More sharing options...
Costanza Posted October 18, 2011 Author Share Posted October 18, 2011 I recall from your previous postings that you are assigning a value to currentOptions, but I don't remember what. Post the code that assigns the values to currentOptions. Also, what line of your code does the error point to? This line assigns the values to currentOptions:currentOptions = document.getElementsByClassName(currentCategory); The error points to the last line of the code I posted inmy OP. This one: currentOptions[color=#676600].[/color]splice[color=#676600]([/color]i[color=#676600],[/color] [color=#066667]1[/color][color=#676600]);[/color] I checked the array length in the console (after running the function) and it is full length. in other words, removeIncompatibleOptions did nothing, weirdly. Link to comment Share on other sites More sharing options...
thescientist Posted October 18, 2011 Share Posted October 18, 2011 ah, that's why. getElementsByClass name returns a node list, which is not the same as a native array.https://developer.mozilla.org/en/DOM/document.getElementsByClassNamehttps://developer.mozilla.org/en/DOM/NodeList Link to comment Share on other sites More sharing options...
Costanza Posted October 18, 2011 Author Share Posted October 18, 2011 ah, that's why. getElementsByClass name returns a node list, which is not the same as a native array.https://developer.mo...entsByClassNamehttps://developer.mo...en/DOM/NodeList Ok cool, so I should be declaring a nodeList instead of an Array, and be using nodeList methods instead of splice()? or is there an equivalent method to getElementsByClassName that returns an array? Or I guess I could do an extra loop first to put the nodeList items into a new array one by one? NodeList doesn't seem to have many methods for altering it. Link to comment Share on other sites More sharing options...
justsomeguy Posted October 18, 2011 Share Posted October 18, 2011 If you want to use array methods, loop through the node list and copy each item to an array. The node list is a live collection, so if you remove an element from the node list it also gets removed from the page. Link to comment Share on other sites More sharing options...
Costanza Posted October 18, 2011 Author Share Posted October 18, 2011 Thanks, that's what I'll do then. Once again thanks for the help guys. This forum is great! Link to comment Share on other sites More sharing options...
thescientist Posted October 18, 2011 Share Posted October 18, 2011 Cooooo-stanza! (can't-stand-ya... ) Link to comment Share on other sites More sharing options...
Costanza Posted October 18, 2011 Author Share Posted October 18, 2011 Cooooo-stanza! (can't-stand-ya... )Oh yeah? Well the jerk store called and they're running out of YOU! Seriously though, I appreciate it. When my little project here is done hopefully I can link it so you guys can see what you've been helping with. It's basically an interface for customizing assault rifles with real parts, similar to what some games do but much more in-depth. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.