1

I have inherited this code that populates the countries in my online checkout. The first value that shows up is Australia but I need it to be set to USA.

<script type=“text/javascript”>
    $(function(){
        $('#FormField_11>option, #FormField_21>option').each(function () {
            // PUT THE COUNTRIES YOU WANT TO KEEP IN THE LIST HERE
            var countries = [ 'United States', 'Canada', 'Belgium', 'Australia', 'Denmark', 'Finland', 'Germany', 'Ireland', 'Italy', 'Japan', 'Netherlands', 'Netherlands Antilles', 'New Zealand', 'Norway', 'Sweden', 'United Kingdom', 'Austria'  ];

            var country = $(this).text();
            if (countries.indexOf(country) == -1) {
                $(this).remove();
            }
        });
    });
</script>​

Can anyone help me define United States as the default?

Thanks in advance

2
  • Do you just want United States selected by default, or if you want it moved to the top of the select? Commented Oct 15, 2019 at 18:37
  • I want the United States selected by default Commented Dec 3, 2019 at 21:58

1 Answer 1

1

After you remove all the unwanted countries, set the values of the dropdowns to the desired default:

$(function(){
    $('#FormField_11>option, #FormField_21>option').each(function () {
        // PUT THE COUNTRIES YOU WANT TO KEEP IN THE LIST HERE
        var countries = [ 'United States', 'Canada', 'Belgium', 'Australia', 'Denmark', 'Finland', 'Germany', 'Ireland', 'Italy', 'Japan', 'Netherlands', 'Netherlands Antilles', 'New Zealand', 'Norway', 'Sweden', 'United Kingdom', 'Austria'  ];

        var country = $(this).text();
        if (countries.indexOf(country) == -1) {
            $(this).remove();
        }
    });
    $("#FormField_11, #FormField_21").value("United States");
});
Sign up to request clarification or add additional context in comments.

2 Comments

That didn't work - - - BUT - I changed it to $("#FormField_11, #FormField_21").value("United States"); because I thought you had a typo in it.
Yes, that was a typo.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.