Jump to content

trouble with onfocus script embedded dropdown menu


boo_lolly

Recommended Posts

alright here's the explanation.

 

i have two dropdown menus in a form. one is a list of countries, the other is a list of states. i want to write a script that will display the states  dropdown only when the country 'america' is selected in the countries dropdown. 'america' is pre-selected by the way. here's what i'm workin with so far. i really don't know much javascript but i think i'm somewhere on the right track. let me know what you guys think.

    <script type="text/javascript">
        function focused()
        {
            statedropdown = '
                <select name="stateslist">
                    <option value="texas">Texas</option>
                    <option value="delaware">Delaware</option>
                    <option value="montana">Montana</option>
                </select>
            ';
            document.getElementById('states').innerText = statedropdown;
        }
        function cleartext()
        {
            document.getElementById('states').innerText = '';
        }
    </script>

    <form>
    <div>
        <select>
            <option value="china">China</option>
            <option value="usa" onfocus="focused()" selected>USA</option>
            <option value="australia">Australia</option>
        </select>
    </div>
    <div id="states">

    </div>
    </form>

 

any help?

Link to comment
Share on other sites

use object literals, it will be very easy to parse and loop through.

 

var countries = {

USA: {

Alabama: 'alabama',

Texas: 'texas'

},

};

 

Then you have you country drop down

<select id ="country_drop"> ...

 

In the head of your doc you will listen for a change in that select element

 

onload = function(){

var country_drop = document.getElementById('country_drop');

var states = document.getElementById('states');

var states_list;

country_drop.onchange = function(){

//go to the country in the countries object

states_list = countries[country_drop.value];

//loop through that list and create the drop from there

for(var x in states_list){

//create your elements here

}

//write your elements to the page

};

};

 

I hope that is a good start for you

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.