Uncaught SyntaxError: Identifier ‘country_states’ has already been declared


This Content is from Stack Overflow. Question asked by Salih Sultan

I am trying to create a form and add “country-state” dynamic list select. The code itself works when I try it apart from the form itself and open it in new html. But when I copy the code into the form itself it gives this error. error link. Could you please help?

<div class="wrapper">
    <select  id="countries">
        <option value="-1">Select Country</option>
        <option value="1">Adana</option>
        <option value="2">Adıyaman</option>

    <select name="" id="states">
        <option value="-1">Select State</option>


<script type="application/javascript">

    let country_states = [
            states :["Aladağ","Ceyhan","Çukurova","Feke","İmamoğlu","Karaisalı","Karataş","Kozan","Pozantı","Saimbeyli","Sarıçam","Seyhan","Tufanbeyli","Yumurtalık","Yüreğir"]
            states :["Merkez","Besni","Çelikhan","Gerger","Gölbaşı","Kahta","Samcak","Sincik","Tut"]    

    let countryselect = document.querySelector('#countries');
    let stateselect = document.querySelector('#states');

    countryselect.onchange = function(){
            for(ele of country_states){
               if(countryselect.value == ele.code) {   

                     let states = ele.states
                     let op= document.createElement('option');  
                     op.value =-1;
                     op.innerText ="Select State"
                     stateselect.options[0] = op;
                     let i=1;
                     for(state of states){
                        let op= document.createElement('option');
                        op.value =state;
                        op.innerText =state;
                        stateselect.options[i] = op;




    // 1,2,3,4,5,6,7
    // 1,2,3,4,5,6


This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?