therealwesfoster Posted March 12, 2009 Share Posted March 12, 2009 Ok, here's the idea. There is a selectbox on the page with all 50 states listed. I'm wanting to remove all the states in the selectbox EXCEPT for the ones I have listed in the keepStates array. Below is my code. I can't get this to work! Either nothing happens or I get a "obj.remove() is not a function" error. <script type="text/javascript"> // This is a simple function to check if a value is in an array Array.prototype.inArray=function(value){var i;for(i=0;i<this.length;i++){if (this[i] == value){return true;}}return false;}; // This is the selectbox object var obj = document.getElementById("selbox"); // Keep: CA, DC, CO, FL, GA, IA, MO, MN, NC, NV, NH, NM, OH, OR, SC, VA, WL var keepStates = new Array('New Mexico','North Carolina','Ohio','Oregon','South Carolina','Virginia'); for(var i=0;i<obj.length;i++){ if (!keepStates.inArray(obj[i].innerHTML)){ // Ok, so the value of the selectbox isn't in the array of states we want to keep, so remove it obj.remove(i); } } </script> This does not work. Please help! Wes Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted March 12, 2009 Share Posted March 12, 2009 Fixed: // This is a simple function to check if a value is in an array Array.prototype.inArray=function(value){var i;for(i=0;i<this.length;i++){if (this[i] == value){return true;}}return false;}; // This is the selectbox object var obj = document.getElementById("selbox"); // Keep: CA, DC, CO, FL, GA, IA, MO, MN, NC, NV, NH, NM, OH, OR, SC, VA, WL var keepStates = ['New Mexico','North Carolina','Ohio','Oregon','South Carolina','Virginia']; for(var i=0;i<obj.length;i++){ if (!keepStates.inArray(obj[i].innerHTML)){ // Ok, so the value of the selectbox isn't in the array of states we want to keep, so remove it obj.remove(i); } } Quote Link to comment Share on other sites More sharing options...
therealwesfoster Posted March 12, 2009 Author Share Posted March 12, 2009 If it were only that easy!! That doesn't work for me.. Any other ideas as to why? Or ideas as to what to do? Wes Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted March 13, 2009 Share Posted March 13, 2009 give us a link then so we can see what is wrong pft Quote Link to comment Share on other sites More sharing options...
therealwesfoster Posted March 13, 2009 Author Share Posted March 13, 2009 I'm doing this as a 1ShoppingCart modification. On the cart page there is a dropdown list of states, but since the product being sold (wine) is limited to where it can be shipped, we're wanting to eliminate some of the state in the dropdown. The script above works in a way. It leaves all the states that it's supposed to leave, but not all of the other states get removed. Just every other one, or every other 2 ones. Does remove() reset the dropdown's index? If so, this could be the problem (because var i is always incrementing) Wes Quote Link to comment Share on other sites More sharing options...
therealwesfoster Posted March 13, 2009 Author Share Posted March 13, 2009 Fixed: // This is a simple function to check if a value is in an array Array.prototype.inArray=function(value){var i;for(i=0;i<this.length;i++){if (this[i] == value){return true;}}return false;}; // This is the selectbox object var obj = document.getElementById("selbox"); // Keep: CA, DC, CO, FL, GA, IA, MO, MN, NC, NV, NH, NM, OH, OR, SC, VA, WL var keepStates = ['New Mexico','North Carolina','Ohio','Oregon','South Carolina','Virginia']; for(var i=0;i<obj.length;i++){ if (!keepStates.inArray(obj[i].innerHTML)){ // Ok, so the value of the selectbox isn't in the array of states we want to keep, so remove it obj.remove(i); } } That removes the whole object, not just the option Wes Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted March 13, 2009 Share Posted March 13, 2009 http://docs.jquery.com/Manipulation/remove problem solved ??? Quote Link to comment Share on other sites More sharing options...
therealwesfoster Posted March 13, 2009 Author Share Posted March 13, 2009 That's a jQuery function I'm needing just javascript Wes Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted March 13, 2009 Share Posted March 13, 2009 thats what im getting at .remove() is a jquery function if you dont want to use it im not sure all what you could do *shrug* Quote Link to comment Share on other sites More sharing options...
therealwesfoster Posted March 13, 2009 Author Share Posted March 13, 2009 FIXED IT! I put i--; in my for loop, this fixed it somehow. Final Code: // This is a simple function to check if a value is in an array Array.prototype.inArray=function(value){var i;for(i=0;i<this.length;i++){if (this[i] == value){return true;}}return false;}; // This is the selectbox object var obj = document.getElementById("selbox"); // Keep: CA, DC, CO, FL, GA, IA, MO, MN, NC, NV, NH, NM, OH, OR, SC, VA, WL var keepStates = ['New Mexico','North Carolina','Ohio','Oregon','South Carolina','Virginia']; for(var i=0;i<obj.length;i++){ if (!keepStates.inArray(obj[i].innerHTML)){ // Ok, so the value of the selectbox isn't in the array of states we want to keep, so remove it obj.remove(i); // THE FIX i--; } } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.