Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7797159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:35:42+00:00 2026-06-01T23:35:42+00:00

I have an HTML select element with some countries as the options. <select id=countryLocation

  • 0

I have an HTML select element with some countries as the options.

<select id="countryLocation" class="selectCountryPrefix enablePartialForm" onchange="document.locationchangeform.submit()" name="country">
    <option selected="selected" disabled="disabled" value="">Select country</option>
    <option class="showTos-AU" title="+61" value="AU">Australia</option>
    <option class="showTos-CA" title="+1" value="CA">Canada</option>
    <option class="showTos-CL" title="+56" value="CL">Chile</option>
    <option class="showTos-DK" title="+45" value="DK">Denmark</option>
    <option class="showTos-EE" title="+372" value="EE">Estonia</option>
    <option class="showTos-FI" title="+358" value="FI">Finland</option>
    <option class="showTos-GR" title="+30" value="GR">Greece</option>
    <option class="showTos-HU" title="+36" value="HU">Hungary</option>
    <option class="showTos-IE" title="+353" value="IE">Ireland</option>
    <option class="default showTos-LV" selected="selected" title="+371" value="LV">Latvia</option>
    <option class="showTos-TW" title="+886" value="TW">Taiwan</option>
</select>

I want to remove all of the options that have a country code not equal to “TW”. So far this is my code:

YAHOO.util.Event.addListener(window, "load", function() {

    var E = YAHOO.util.Event;
    var D = YAHOO.util.Dom;

    var opts,
        select,
        i;

    select = D.get('countryLocation');
    opts = select.options;
    for (i=0; i< opts.length-1; i++){
        if (opts[i].value !== ""){
            if (opts[i].value !== "TW"){
                select.remove(i);
            }
        } 
    }

})

it only works on some of the options – I’m guessing that because it’s removing them by index then the indexes are changing after each one is removed. What I really want it something like the jQuery .each() function.

How can I do this either in YUI or just plain JS?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T23:35:44+00:00Added an answer on June 1, 2026 at 11:35 pm

    Another approach (in regard to Brant Olsen’s answer) is to iterate backwards:

    for (i=opts.length-1; i >= 0; i--){
        if (opts[i].value !== ""){
            if (opts[i].value !== "TW"){
                select.remove(i);
            }
        } 
    }
    

    I usually go with this approach when deleting objects from an array (within an iteration).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a html select with options in my views . onchange i want
I have some long text in a html select element. When a user selects
I have some html element like these: <table id=myTable></table> <select name=mySelect> <option value=1>1</option> <option
Does the HTML select element have an on select event? what exactly is the
I have a html element (like select box input field) in a table. Now
I have a html combo/select. When the user selects a element, I want to
I have the following HTML <select> element: <select id=leaveCode name=leaveCode> <option value=10>Annual Leave</option> <option
I have a HTML option element that I want to perform some actions when
Suppose I have a <select> element on my HTML page, and I want to
I have several 'select' elements on the page. When I choose some of options,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.