Monday 23 July 2018

Add States in Dropdown When Nation Is Selected | Add Values In Dropdown box Using Javascript | HTML tutorial

Code :-
<html>
    <head>
        <script>
            function random_function()
            {
                var a=document.getElementById("input").value;
                if(a==="INDIA")
                {
                    var arr=["Maharashtra","Delhi"];
                }
                else if(a==="USA")
                {
                    var arr=["Washington","Texas","New York"];
                }
             
                var string="";
             
                for(i=0;i<arr.length;i++)
                {
                    string=string+"<option value="+arr[i]+">"+arr[i]+"</option>";
                }
                document.getElementById("output").innerHTML=string;
            }
        </script>
    </head>
    <body>
        <select id="input" onchange="random_function()">
            <option>select option</option>
            <option>INDIA</option>
            <option>USA</option>
        </select>
        <div>
           <select id="output" onchange="random_function1()">
        </div>
    </body>
</html>
Dialogs :-

Hey guys, this is unpossible pog.
Today’s tutorial will be how to add values in dropdown when we select other dropdown.in this case we are adding states or cities in dropdown when you select contries.
First create a normal dorpdown, give it an ID as “input”.
I shall add nations in it.
Let’s check it.
Now add script tag, and create a function with any random name.
In select tag, onchange event, call the function name.
Now, the function is called correctly.
But we don’t need alert, so I shall remove it.
Lets create a variable “a” to get nation name from dropdown.
Then create division and give it an ID as “output”.
Lets print the nation’s name.
So far so good.
Now we need if else statements.
Give equals to symbol “three times”.
Add states in array.
We need a string and for loop.
We are appending the string.
Change “a” to “string”.
Let’s check if all states are coming or not.
Before and after array, add option tag in double quote.
Add name if you want to send it through form.
Add select tag inside div just to make an illusion.
So thats it guys, if you want code, then the link is in the description.
don’t forget to Like,share and subscribe. Thank you for watching, bye.





9 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Thank you so much. Actually it worked for me.

    ReplyDelete
  3. how to make this work as a form? use the values selected and pass it to "post". Thank you

    ReplyDelete
  4. hi can you tell me this i need two drop down boxes on country and an other language when we selected country and language enable button wen we selected default option select country button want disable

    ReplyDelete
  5. Hi, thank you for helping us.
    Please who can help?
    I need to create State, county, city, state and county I have but missing the box for the city please for help. Well thank you

    ReplyDelete
  6. I am using django.db when I insert any state with space it take only before space character pls help

    ReplyDelete
  7. I should disable until country is selected and if country is selected state should have three options

    ReplyDelete
  8. plz mujhe btao ki isme agar ham 2 se jyada values add kare to kaise karenge (like: country, state, and city). plz btana jarur sir. agar ho sake to plz sir jba hindi me dena
    vaise aapka ye code lajvab hai is tarah ke code ki m kfi dino se tlas kar raha tha aapki video aur post ne me bahot jyada help ki hai... isiliye sir. aapko dil se dhanyavaad

    ReplyDelete