Jump to content

[SOLVED] Removing a selectbox option


therealwesfoster

Recommended Posts

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

Link to comment
Share on other sites

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);
   }
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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--;
   }
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.