Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Jun 2013
    Posts
    14
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Javascript Selected Value doesn't work

    I am trying to select an index for selected value using javascript which matches one of the value in the database based on what the user typed in previously but the problem is nothing really happen at all. Here's the code for reference:

    Code:
    <script type="text/javascript">
            function ChoiceSelect()
    {
            var country = <?php echo $row['user_country'];?>
    	var selectobject=document.getElementById("countryselect")
            var index = 0;
            for (var i=0; i<selectobject.length; i++){
            if(selectobject.options[i].text==country){
                index = i;
            }
            selectobject.selectedIndex = index;     
            }
    }
            </script>
    Here's part of the code from my PHP page

    PHP Code:
    <body onload="javascript:ChoiceSelect()">

    <
    td><select id ="countryselect">
        <
    option value="Afghanistan" title="Afghanistan">Afghanistan</option>
        <
    option value="┼land Islands" title="┼land Islands">Aland Islands</option>
        <
    option value="Albania" title="Albania">Albania</option>
        <
    option value="Algeria" title="Algeria">Algeria</option>
        <
    option value="American Samoa" title="American Samoa">American Samoa</option>
        <
    option value="Andorra" title="Andorra">Andorra</option>
        <
    option value="Angola" title="Angola">Angola</option>
        <
    option value="Anguilla" title="Anguilla">Anguilla</option>
        <
    option value="Antarctica" title="Antarctica">Antarctica</option>
        <
    option value="Antigua and Barbuda" title="Antigua and Barbuda">Antigua and Barbuda</option>
        <
    option value="Argentina" title="Argentina">Argentina</option>
        <
    option value="Armenia" title="Armenia">Armenia</option>
        <
    option value="Aruba" title="Aruba">Aruba</option>
        <
    option value="Australia" title="Australia">Australia</option>
        <
    option value="Austria" title="Austria">Austria</option>
        <
    option value="Azerbaijan" title="Azerbaijan">Azerbaijan</option>
        <
    option value="Bahamas" title="Bahamas">Bahamas</option>
        <
    option value="Bahrain" title="Bahrain">Bahrain</option>
        <
    option value="Bangladesh" title="Bangladesh">Bangladesh</option>
        <
    option value="Barbados" title="Barbados">Barbados</option>
        <
    option value="Belarus" title="Belarus">Belarus</option>
        <
    option value="Belgium" title="Belgium">Belgium</option>
        <
    option value="Belize" title="Belize">Belize</option>
        <
    option value="Benin" title="Benin">Benin</option>
        <
    option value="Bermuda" title="Bermuda">Bermuda</option>
        <
    option value="Bhutan" title="Bhutan">Bhutan</option>
        <
    option value="Bolivia, Plurinational State of" title="Bolivia, Plurinational State of">BoliviaPlurinational State of</option>
        <
    option value="Bonaire, Sint Eustatius and Saba" title="Bonaire, Sint Eustatius and Saba">BonaireSint Eustatius and Saba</option>
        <
    option value="Bosnia and Herzegovina" title="Bosnia and Herzegovina">Bosnia and Herzegovina</option>
        <
    option value="Botswana" title="Botswana">Botswana</option>
        <
    option value="Bouvet Island" title="Bouvet Island">Bouvet Island</option>
        <
    option value="Brazil" title="Brazil">Brazil</option>
        <
    option value="British Indian Ocean Territory" title="British Indian Ocean Territory">British Indian Ocean Territory</option>
        <
    option value="Brunei Darussalam" title="Brunei Darussalam">Brunei Darussalam</option>
        <
    option value="Bulgaria" title="Bulgaria">Bulgaria</option>
        <
    option value="Burkina Faso" title="Burkina Faso">Burkina Faso</option>
        <
    option value="Burundi" title="Burundi">Burundi</option>
        <
    option value="Cambodia" title="Cambodia">Cambodia</option>
        <
    option value="Cameroon" title="Cameroon">Cameroon</option>
        <
    option value="Canada" title="Canada">Canada</option>
        <
    option value="Cape Verde" title="Cape Verde">Cape Verde</option>
        <
    option value="Cayman Islands" title="Cayman Islands">Cayman Islands</option>
        <
    option value="Central African Republic" title="Central African Republic">Central African Republic</option>
        <
    option value="Chad" title="Chad">Chad</option>
        <
    option value="Chile" title="Chile">Chile</option>
        <
    option value="China" title="China">China</option>
        <
    option value="Christmas Island" title="Christmas Island">Christmas Island</option>
        <
    option value="Cocos (Keeling) Islands" title="Cocos (Keeling) Islands">Cocos (KeelingIslands</option>
        <
    option value="Colombia" title="Colombia">Colombia</option>
        <
    option value="Comoros" title="Comoros">Comoros</option>
        <
    option value="Congo" title="Congo">Congo</option>
        <
    option value="Congo, the Democratic Republic of the" title="Congo, the Democratic Republic of the">Congothe Democratic Republic of the</option>
        <
    option value="Cook Islands" title="Cook Islands">Cook Islands</option>
        <
    option value="Costa Rica" title="Costa Rica">Costa Rica</option>
        <
    option value="C˘te d'Ivoire" title="C˘te d'Ivoire">C˘te d'Ivoire</option>
        <option value="Croatia" title="Croatia">Croatia</option>
        <option value="Cuba" title="Cuba">Cuba</option>
        <option value="Curašao" title="Curašao">Curašao</option>
        <option value="Cyprus" title="Cyprus">Cyprus</option>
        <option value="Czech Republic" title="Czech Republic">Czech Republic</option>
        <option value="Denmark" title="Denmark">Denmark</option>
        <option value="Djibouti" title="Djibouti">Djibouti</option>
        <option value="Dominica" title="Dominica">Dominica</option>
        <option value="Dominican Republic" title="Dominican Republic">Dominican Republic</option>
        <option value="Ecuador" title="Ecuador">Ecuador</option>
        <option value="Egypt" title="Egypt">Egypt</option>
        <option value="El Salvador" title="El Salvador">El Salvador</option>
        <option value="Equatorial Guinea" title="Equatorial Guinea">Equatorial Guinea</option>
        <option value="Eritrea" title="Eritrea">Eritrea</option>
        <option value="Estonia" title="Estonia">Estonia</option>
        <option value="Ethiopia" title="Ethiopia">Ethiopia</option>
        <option value="Falkland Islands (Malvinas)" title="Falkland Islands (Malvinas)">Falkland Islands (Malvinas)</option>
        <option value="Faroe Islands" title="Faroe Islands">Faroe Islands</option>
        <option value="Fiji" title="Fiji">Fiji</option>
        <option value="Finland" title="Finland">Finland</option>
        <option value="France" title="France">France</option>
        <option value="French Guiana" title="French Guiana">French Guiana</option>
        <option value="French Polynesia" title="French Polynesia">French Polynesia</option>
        <option value="French Southern Territories" title="French Southern Territories">French Southern Territories</option>
        <option value="Gabon" title="Gabon">Gabon</option>
        <option value="Gambia" title="Gambia">Gambia</option>
        <option value="Georgia" title="Georgia">Georgia</option>
        <option value="Germany" title="Germany">Germany</option>
        <option value="Ghana" title="Ghana">Ghana</option>
        <option value="Gibraltar" title="Gibraltar">Gibraltar</option>
        <option value="Greece" title="Greece">Greece</option>
        <option value="Greenland" title="Greenland">Greenland</option>
        <option value="Grenada" title="Grenada">Grenada</option>
        <option value="Guadeloupe" title="Guadeloupe">Guadeloupe</option>
        <option value="Guam" title="Guam">Guam</option>
        <option value="Guatemala" title="Guatemala">Guatemala</option>
        <option value="Guernsey" title="Guernsey">Guernsey</option>
        <option value="Guinea" title="Guinea">Guinea</option>
        <option value="Guinea-Bissau" title="Guinea-Bissau">Guinea-Bissau</option>
        <option value="Guyana" title="Guyana">Guyana</option>
        <option value="Haiti" title="Haiti">Haiti</option>
        <option value="Heard Island and McDonald Islands" title="Heard Island and McDonald Islands">Heard Island and McDonald Islands</option>
        <option value="Holy See (Vatican City State)" title="Holy See (Vatican City State)">Holy See (Vatican City State)</option>
        <option value="Honduras" title="Honduras">Honduras</option>
        <option value="Hong Kong" title="Hong Kong">Hong Kong</option>
        <option value="Hungary" title="Hungary">Hungary</option>
        <option value="Iceland" title="Iceland">Iceland</option>
        <option value="India" title="India">India</option>
        <option value="Indonesia" title="Indonesia">Indonesia</option>
        <option value="Iran, Islamic Republic of" title="Iran, Islamic Republic of">Iran, Islamic Republic of</option>
        <option value="Iraq" title="Iraq">Iraq</option>
        <option value="Ireland" title="Ireland">Ireland</option>
        <option value="Isle of Man" title="Isle of Man">Isle of Man</option>
        <option value="Israel" title="Israel">Israel</option>
        <option value="Italy" title="Italy">Italy</option>
        <option value="Jamaica" title="Jamaica">Jamaica</option>
        <option value="Japan" title="Japan">Japan</option>
        <option value="Jersey" title="Jersey">Jersey</option>
        <option value="Jordan" title="Jordan">Jordan</option>
        <option value="Kazakhstan" title="Kazakhstan">Kazakhstan</option>
        <option value="Kenya" title="Kenya">Kenya</option>
        <option value="Kiribati" title="Kiribati">Kiribati</option>
        <option value="Korea, Democratic People'
    s Republic of" title="KoreaDemocratic People's Republic of">Korea, Democratic People's Republic of</option>
        <
    option value="Korea, Republic of" title="Korea, Republic of">KoreaRepublic of</option>
        <
    option value="Kuwait" title="Kuwait">Kuwait</option>
        <
    option value="Kyrgyzstan" title="Kyrgyzstan">Kyrgyzstan</option>
        <
    option value="Lao People's Democratic Republic" title="Lao People's Democratic Republic">Lao Peoples Democratic Republic</option>
        <
    option value="Latvia" title="Latvia">Latvia</option>
        <
    option value="Lebanon" title="Lebanon">Lebanon</option>
        <
    option value="Lesotho" title="Lesotho">Lesotho</option>
        <
    option value="Liberia" title="Liberia">Liberia</option>
        <
    option value="Libya" title="Libya">Libya</option>
        <
    option value="Liechtenstein" title="Liechtenstein">Liechtenstein</option>
        <
    option value="Lithuania" title="Lithuania">Lithuania</option>
        <
    option value="Luxembourg" title="Luxembourg">Luxembourg</option>
        <
    option value="Macao" title="Macao">Macao</option>
        <
    option value="Macedonia, the former Yugoslav Republic of" title="Macedonia, the former Yugoslav Republic of">Macedoniathe former Yugoslav Republic of</option>
        <
    option value="Madagascar" title="Madagascar">Madagascar</option>
        <
    option value="Malawi" title="Malawi">Malawi</option>
        <
    option value="Malaysia" title="Malaysia">Malaysia</option>
        <
    option value="Maldives" title="Maldives">Maldives</option>
        <
    option value="Mali" title="Mali">Mali</option>
        <
    option value="Malta" title="Malta">Malta</option>
        <
    option value="Marshall Islands" title="Marshall Islands">Marshall Islands</option>
        <
    option value="Martinique" title="Martinique">Martinique</option>
        <
    option value="Mauritania" title="Mauritania">Mauritania</option>
        <
    option value="Mauritius" title="Mauritius">Mauritius</option>
        <
    option value="Mayotte" title="Mayotte">Mayotte</option>
        <
    option value="Mexico" title="Mexico">Mexico</option>
        <
    option value="Micronesia, Federated States of" title="Micronesia, Federated States of">MicronesiaFederated States of</option>
        <
    option value="Moldova, Republic of" title="Moldova, Republic of">MoldovaRepublic of</option>
        <
    option value="Monaco" title="Monaco">Monaco</option>
        <
    option value="Mongolia" title="Mongolia">Mongolia</option>
        <
    option value="Montenegro" title="Montenegro">Montenegro</option>
        <
    option value="Montserrat" title="Montserrat">Montserrat</option>
        <
    option value="Morocco" title="Morocco">Morocco</option>
        <
    option value="Mozambique" title="Mozambique">Mozambique</option>
        <
    option value="Myanmar" title="Myanmar">Myanmar</option>
        <
    option value="Namibia" title="Namibia">Namibia</option>
        <
    option value="Nauru" title="Nauru">Nauru</option>
        <
    option value="Nepal" title="Nepal">Nepal</option>
        <
    option value="Netherlands" title="Netherlands">Netherlands</option>
        <
    option value="New Caledonia" title="New Caledonia">New Caledonia</option>
        <
    option value="New Zealand" title="New Zealand">New Zealand</option>
        <
    option value="Nicaragua" title="Nicaragua">Nicaragua</option>
        <
    option value="Niger" title="Niger">Niger</option>
        <
    option value="Nigeria" title="Nigeria">Nigeria</option>
        <
    option value="Niue" title="Niue">Niue</option>
        <
    option value="Norfolk Island" title="Norfolk Island">Norfolk Island</option>
        <
    option value="Northern Mariana Islands" title="Northern Mariana Islands">Northern Mariana Islands</option>
        <
    option value="Norway" title="Norway">Norway</option>
        <
    option value="Oman" title="Oman">Oman</option>
        <
    option value="Pakistan" title="Pakistan">Pakistan</option>
        <
    option value="Palau" title="Palau">Palau</option>
        <
    option value="Palestinian Territory, Occupied" title="Palestinian Territory, Occupied">Palestinian TerritoryOccupied</option>
        <
    option value="Panama" title="Panama">Panama</option>
        <
    option value="Papua New Guinea" title="Papua New Guinea">Papua New Guinea</option>
        <
    option value="Paraguay" title="Paraguay">Paraguay</option>
        <
    option value="Peru" title="Peru">Peru</option>
        <
    option value="Philippines" title="Philippines">Philippines</option>
        <
    option value="Pitcairn" title="Pitcairn">Pitcairn</option>
        <
    option value="Poland" title="Poland">Poland</option>
        <
    option value="Portugal" title="Portugal">Portugal</option>
        <
    option value="Puerto Rico" title="Puerto Rico">Puerto Rico</option>
        <
    option value="Qatar" title="Qatar">Qatar</option>
        <
    option value="RÚunion" title="RÚunion">RÚunion</option>
        <
    option value="Romania" title="Romania">Romania</option>
        <
    option value="Russian Federation" title="Russian Federation">Russian Federation</option>
        <
    option value="Rwanda" title="Rwanda">Rwanda</option>
        <
    option value="Saint BarthÚlemy" title="Saint BarthÚlemy">Saint BarthÚlemy</option>
        <
    option value="Saint Helena, Ascension and Tristan da Cunha" title="Saint Helena, Ascension and Tristan da Cunha">Saint HelenaAscension and Tristan da Cunha</option>
        <
    option value="Saint Kitts and Nevis" title="Saint Kitts and Nevis">Saint Kitts and Nevis</option>
        <
    option value="Saint Lucia" title="Saint Lucia">Saint Lucia</option>
        <
    option value="Saint Martin (French part)" title="Saint Martin (French part)">Saint Martin (French part)</option>
        <
    option value="Saint Pierre and Miquelon" title="Saint Pierre and Miquelon">Saint Pierre and Miquelon</option>
        <
    option value="Saint Vincent and the Grenadines" title="Saint Vincent and the Grenadines">Saint Vincent and the Grenadines</option>
        <
    option value="Samoa" title="Samoa">Samoa</option>
        <
    option value="San Marino" title="San Marino">San Marino</option>
        <
    option value="Sao Tome and Principe" title="Sao Tome and Principe">Sao Tome and Principe</option>
        <
    option value="Saudi Arabia" title="Saudi Arabia">Saudi Arabia</option>
        <
    option value="Senegal" title="Senegal">Senegal</option>
        <
    option value="Serbia" title="Serbia">Serbia</option>
        <
    option value="Seychelles" title="Seychelles">Seychelles</option>
        <
    option value="Sierra Leone" title="Sierra Leone">Sierra Leone</option>
        <
    option value="Singapore" title="Singapore">Singapore</option>
        <
    option value="Sint Maarten (Dutch part)" title="Sint Maarten (Dutch part)">Sint Maarten (Dutch part)</option>
        <
    option value="Slovakia" title="Slovakia">Slovakia</option>
        <
    option value="Slovenia" title="Slovenia">Slovenia</option>
        <
    option value="Solomon Islands" title="Solomon Islands">Solomon Islands</option>
        <
    option value="Somalia" title="Somalia">Somalia</option>
        <
    option value="South Africa" title="South Africa">South Africa</option>
        <
    option value="South Georgia and the South Sandwich Islands" title="South Georgia and the South Sandwich Islands">South Georgia and the South Sandwich Islands</option>
        <
    option value="South Sudan" title="South Sudan">South Sudan</option>
        <
    option value="Spain" title="Spain">Spain</option>
        <
    option value="Sri Lanka" title="Sri Lanka">Sri Lanka</option>
        <
    option value="Sudan" title="Sudan">Sudan</option>
        <
    option value="Suriname" title="Suriname">Suriname</option>
        <
    option value="Svalbard and Jan Mayen" title="Svalbard and Jan Mayen">Svalbard and Jan Mayen</option>
        <
    option value="Swaziland" title="Swaziland">Swaziland</option>
        <
    option value="Sweden" title="Sweden">Sweden</option>
        <
    option value="Switzerland" title="Switzerland">Switzerland</option>
        <
    option value="Syrian Arab Republic" title="Syrian Arab Republic">Syrian Arab Republic</option>
        <
    option value="Taiwan, Province of China" title="Taiwan, Province of China">TaiwanProvince of China</option>
        <
    option value="Tajikistan" title="Tajikistan">Tajikistan</option>
        <
    option value="Tanzania, United Republic of" title="Tanzania, United Republic of">TanzaniaUnited Republic of</option>
        <
    option value="Thailand" title="Thailand">Thailand</option>
        <
    option value="Timor-Leste" title="Timor-Leste">Timor-Leste</option>
        <
    option value="Togo" title="Togo">Togo</option>
        <
    option value="Tokelau" title="Tokelau">Tokelau</option>
        <
    option value="Tonga" title="Tonga">Tonga</option>
        <
    option value="Trinidad and Tobago" title="Trinidad and Tobago">Trinidad and Tobago</option>
        <
    option value="Tunisia" title="Tunisia">Tunisia</option>
        <
    option value="Turkey" title="Turkey">Turkey</option>
        <
    option value="Turkmenistan" title="Turkmenistan">Turkmenistan</option>
        <
    option value="Turks and Caicos Islands" title="Turks and Caicos Islands">Turks and Caicos Islands</option>
        <
    option value="Tuvalu" title="Tuvalu">Tuvalu</option>
        <
    option value="Uganda" title="Uganda">Uganda</option>
        <
    option value="Ukraine" title="Ukraine">Ukraine</option>
        <
    option value="United Arab Emirates" title="United Arab Emirates">United Arab Emirates</option>
        <
    option value="United Kingdom" title="United Kingdom">United Kingdom</option>
        <
    option value="United States" title="United States">United States</option>
        <
    option value="United States Minor Outlying Islands" title="United States Minor Outlying Islands">United States Minor Outlying Islands</option>
        <
    option value="Uruguay" title="Uruguay">Uruguay</option>
        <
    option value="Uzbekistan" title="Uzbekistan">Uzbekistan</option>
        <
    option value="Vanuatu" title="Vanuatu">Vanuatu</option>
        <
    option value="Venezuela, Bolivarian Republic of" title="Venezuela, Bolivarian Republic of">VenezuelaBolivarian Republic of</option>
        <
    option value="Viet Nam" title="Viet Nam">Viet Nam</option>
        <
    option value="Virgin Islands, British" title="Virgin Islands, British">Virgin IslandsBritish</option>
        <
    option value="Virgin Islands, U.S." title="Virgin Islands, U.S.">Virgin IslandsU.S.</option>
        <
    option value="Wallis and Futuna" title="Wallis and Futuna">Wallis and Futuna</option>
        <
    option value="Western Sahara" title="Western Sahara">Western Sahara</option>
        <
    option value="Yemen" title="Yemen">Yemen</option>
        <
    option value="Zambia" title="Zambia">Zambia</option>
        <
    option value="Zimbabwe" title="Zimbabwe">Zimbabwe</option>
            </
    select></td
    Can anyone give me some assistance? Any help would be appreciated, thanks!

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,594
    Thanks
    23
    Thanked 646 Times in 645 Posts
    There is a far easier way to do what I think you want.
    You don't need an onload="javascript:ChoiceSelect()"
    Use onchange in the select tag.
    Pass the value to the JS. That will eliminate the time consuming loop looking for it.
    Definitely get rid of the <td> tags

    I added the Choose a country option is the onchange would work for the first choice.
    Code:
    <body>
    <select id ="countryselect" onchange="ChoiceSelect(this.value);">
        <option value="">Choose a country</option>
        <option value="Afghanistan" title="Afghanistan">Afghanistan</option>
        <option value="┼land Islands" title="┼land Islands">Aland Islands</option>
        <option value="Albania" title="Albania">Albania</option>
        <option value="Algeria" title="Algeria">Algeria</option>
        <option value="American Samoa" title="American Samoa">American Samoa</option>
        
    </select>
    
    <script type="text/javascript">
    function ChoiceSelect(aplace){
    alert("Index is: "+document.getElementById("countryselect").selectedIndex);
    alert(aplace);
    }
    </script>
    </body>
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • Users who have thanked sunfighter for this post:

    alucard786 (08-02-2013)

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Even better would be to make the JavaScript unobtrusive.

    Code:
    <body>
    <select id ="countryselect">
        <option value="">Choose a country</option>
        <option value="Afghanistan" title="Afghanistan">Afghanistan</option>
        <option value="┼land Islands" title="┼land Islands">Aland Islands</option>
        <option value="Albania" title="Albania">Albania</option>
        <option value="Algeria" title="Algeria">Algeria</option>
        <option value="American Samoa" title="American Samoa">American Samoa</option>
        
    </select>
    
    <script type="text/javascript">
    (function() {
    document.getElementById('countryselect').onchange = fuunction() {ChoiceSelect(this.value)};
    function ChoiceSelect(aplace){
    alert("Index is: "+document.getElementById("countryselect").selectedIndex);
    alert(aplace);
    }
    })();
    </script>
    </body>
    plus of course you'd replace those debugging alert calls with whatever you want tto actually happen once you are sure that the code works and before the page is put on the web.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • Users who have thanked felgall for this post:

    alucard786 (08-02-2013)

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Code:
    (function() {
        document.getElementById('countryselect').onchange = function(e) {
            alert("Index is: "+this.selectedIndex);
        }
    })();
    But it's best to do it via PHP. Javascript is not needed at all.
    PHP Code:
    <?php
    $country 
    $row['user_country'];
    ?>
    <option value="Afghanistan" title="Afghanistan"<php? if ($country == 'Afghanistan') echo 
    ' selected="selected"'; ?>>Afghanistan</option>
    <option value="┼land Islands" title="┼land Islands"<php? if ($country == '┼land Islands') echo 
    ' selected="selected"'; ?>>┼land Islands</option>
    ...and so on
    Not tested so there might be php syntax errors there but you get the point.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • Users who have thanked glenngv for this post:

    alucard786 (08-02-2013)

  • #5
    New Coder
    Join Date
    Jun 2013
    Posts
    14
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by sunfighter View Post
    There is a far easier way to do what I think you want.
    You don't need an onload="javascript:ChoiceSelect()"
    Use onchange in the select tag.
    Pass the value to the JS. That will eliminate the time consuming loop looking for it.
    Definitely get rid of the <td> tags

    I added the Choose a country option is the onchange would work for the first choice.
    Code:
    <body>
    <select id ="countryselect" onchange="ChoiceSelect(this.value);">
        <option value="">Choose a country</option>
        <option value="Afghanistan" title="Afghanistan">Afghanistan</option>
        <option value="┼land Islands" title="┼land Islands">Aland Islands</option>
        <option value="Albania" title="Albania">Albania</option>
        <option value="Algeria" title="Algeria">Algeria</option>
        <option value="American Samoa" title="American Samoa">American Samoa</option>
        
    </select>
    
    <script type="text/javascript">
    function ChoiceSelect(aplace){
    alert("Index is: "+document.getElementById("countryselect").selectedIndex);
    alert(aplace);
    }
    </script>
    </body>
    Quote Originally Posted by felgall View Post
    Even better would be to make the JavaScript unobtrusive.

    Code:
    <body>
    <select id ="countryselect">
        <option value="">Choose a country</option>
        <option value="Afghanistan" title="Afghanistan">Afghanistan</option>
        <option value="┼land Islands" title="┼land Islands">Aland Islands</option>
        <option value="Albania" title="Albania">Albania</option>
        <option value="Algeria" title="Algeria">Algeria</option>
        <option value="American Samoa" title="American Samoa">American Samoa</option>
        
    </select>
    
    <script type="text/javascript">
    (function() {
    document.getElementById('countryselect').onchange = fuunction() {ChoiceSelect(this.value)};
    function ChoiceSelect(aplace){
    alert("Index is: "+document.getElementById("countryselect").selectedIndex);
    alert(aplace);
    }
    })();
    </script>
    </body>
    plus of course you'd replace those debugging alert calls with whatever you want tto actually happen once you are sure that the code works and before the page is put on the web.
    Quote Originally Posted by glenngv View Post
    Code:
    (function() {
        document.getElementById('countryselect').onchange = function(e) {
            alert("Index is: "+this.selectedIndex);
        }
    })();
    But it's best to do it via PHP. Javascript is not needed at all.
    PHP Code:
    <?php
    $country 
    $row['user_country'];
    ?>
    <option value="Afghanistan" title="Afghanistan"<php? if ($country == 'Afghanistan') echo 
    ' selected="selected"'; ?>>Afghanistan</option>
    <option value="┼land Islands" title="┼land Islands"<php? if ($country == '┼land Islands') echo 
    ' selected="selected"'; ?>>┼land Islands</option>
    ...and so on
    Not tested so there might be php syntax errors there but you get the point.
    Hi thanks for all of the constructive feedback, I really appreciate it. I guess that glenngv is more of what I want since I want the value that the user has selected previously before to be reflected in the drop down list. However, because there are 100 and thousands of countries in my drop down list, I am not that fond of hard-coding every single one of them.

    My question, is there any more efficient way using javascript or php to get the same results? Thanks again for the swift response

  • #6
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Thousands of countries? Last time I checked, there only 196 countries in the world.

    Anyway, I suggest you put the countries in a php array and then loop through it.

    PHP Code:
    <?php 
    $user_country 
    $row['user_country']; 
    $countries = array(
        
    'Afghanistan' => 'Afghanistan',
        
    '┼land Islands' => '┼land Islands',
        
    //...and so on
    );
    foreach (
    $countries AS $key=>$country){
         echo 
    '<option value="' $key '"' . ($key == $user_country ' selected="selected"')  . '>' $key '</option>';
    }
    ?>
    Again not tested.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • Users who have thanked glenngv for this post:

    alucard786 (08-03-2013)

  • #7
    New Coder
    Join Date
    Jun 2013
    Posts
    14
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by glenngv View Post
    Thousands of countries? Last time I checked, there only 196 countries in the world.

    Anyway, I suggest you put the countries in a php array and then loop through it.

    PHP Code:
    <?php 
    $user_country 
    $row['user_country']; 
    $countries = array(
        
    'Afghanistan' => 'Afghanistan',
        
    '┼land Islands' => '┼land Islands',
        
    //...and so on
    );
    foreach (
    $countries AS $key=>$country){
         echo 
    '<option value="' $key '"' . ($key == $user_country ' selected="selected"')  . '>' $key '</option>';
    }
    ?>
    Again not tested.
    Haha, it's just a figure of expression

    I guess I will have to use this method then, since I don't think there are better methods to substitute this. Thanks again!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •