user1732364
user1732364

Reputation: 985

Set Dropdownlist selected index with javascript?

I have 2 dropdownlists on a asp page.

If user changes the selected index of the first drop down list, then set DDL2.selectedindex = DDL1.Selectedindex

and do this same logic except switch DDL1 and DDL2 respectively. I have these both getting populated from the same list of objects(just different properties set to each) and i have a order by clause on the query to ensure the data stays in sync. My question is how can i get this logic to work in javascript? My current method is as such..

Accounts.Attributes.Add("onBlur", Customers.SelectedIndex = Accounts.SelectedIndex)
Customers.Attributes.Add("onBlur", Accounts.SelectedIndex = Customers.SelectedIndex)

This code doesn't work but demonstrates what im shooting for. When the ddl getting the first selection loses focus, populate the other ddl(setting the selected index). Any help would be great!


Can someone see what i'm doing wrong here?

            $("[id$=ddlStandardAcctNo]").change(function () {

            var acc = $("[id$=ddlStandardAcctNo]");
            var cust = $("[id$=ddlCustomerName]");
            cust.selectedindex = acc.selectedindex;
        });

It compiles and just doesn't work... :( These drop downs are inside of a asp gridview.


After looking at that i'm trying to do this..

        $("[id$=ddlStandardAcctNo]").blur(function () {

        var acc = document.getElementById('<%=ddlStandardAcctNo.ClientID %>');
        var cust = document.getElementById('<%=ddlCustomerName.ClientID %>');
        cust.selectedindex = acc.selectedindex
    });

    $("[id$=ddlCustomerName]").blur(function () {

        var acc = document.getElementById('<%=ddlStandardAcctNo.ClientID %>');
        var cust = document.getElementById('<%=ddlCustomerName.ClientID %>');
        acc.selectedindex = cust.selectedindex
    });

Problem is i never use document.ready cause the dropdownlist are in a gridview. I'm literally just learning javascript/jquery as i run across issues like this so feel free to crack the knowledge whip lol.

Upvotes: 1

Views: 13688

Answers (1)

user1732364
user1732364

Reputation: 985

I figured this out finally!!!! the solution for jquery prior is the following

                        $("[id$=ddlStandardAcctNo]").change(function () {
                        $("[id$=ddlCustomerName]").attr("selectedIndex", this.selectedIndex);
                    });

                    $("[id$=ddlCustomerName]").change(function () {
                        $("[id$=ddlStandardAcctNo]").attr("selectedIndex", this.selectedIndex);
                    });

Upvotes: 1

Related Questions