Home:ALL Converter>Set Dropdownlist selected index with javascript?

Set Dropdownlist selected index with javascript?

Ask Time:2012-11-20T05:15:45         Author:user1732364

Json Formatter

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.

Author:user1732364,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/13462383/set-dropdownlist-selected-index-with-javascript
yy