Selected Index of a DropDownList

1 Answer 3967 Views
DropDownList
David
Top achievements
Rank 1
David asked on 05 Sep 2012, 09:32 PM
How do I get the selected index of a DropDownList?  I don't see anything in the API docs.

1 Answer, 1 is accepted

Sort by
1
Nohinn
Top achievements
Rank 1
answered on 06 Sep 2012, 02:06 PM
In the select callback of the dropdownlist:
$('#input').kendoDropDownList({
    select: function(e) {
        var index = e.item.index();
    }
});

You have a sample here too:
http://demos.kendoui.com/web/dropdownlist/events.html 
David
Top achievements
Rank 1
commented on 06 Sep 2012, 03:43 PM

That works in the select function and helps me in one of the 2 cases.  I still don't see how I can arbitrarily get the selected index for the DropDownList (outside of the select function).
Nohinn
Top achievements
Rank 1
commented on 06 Sep 2012, 03:47 PM

var index = $('#input').data('kendoDropDownList').select();

That would be with the dropdown closed without using the select callback, and is zero-based. 
Hari
Top achievements
Rank 1
commented on 03 Oct 2012, 05:53 AM

Works perfectly! Thanks Nohinn.
Michael
Top achievements
Rank 1
commented on 25 Aug 2021, 03:25 PM

This worked wonderfully, but one thing to remember is if there is an option label, it means the index of the item ($('#input').data('kendoDropDownList').select();) is not the same as the index in the dataSource.  Discovered this when trying t remove a selected item from a datasource after adding it to another list.
Neli
Telerik team
commented on 30 Aug 2021, 10:31 AM

Hi Michael,

indeed, when the optionLabel is used in the configuration, its value is placed in the DropDownList popup as well under index 0. 

Regards.

Lee
Top achievements
Rank 1
Iron
Iron
Iron
commented on 01 Apr 2022, 05:28 PM

How would this work outside of an event listener. For example, how would I create a function called "getIndex" and run that from the console?
Michael
Top achievements
Rank 1
commented on 01 Apr 2022, 06:22 PM

@Lee - did you see the comment from @Nohinn from 2012 above?
Lee
Top achievements
Rank 1
Iron
Iron
Iron
commented on 01 Apr 2022, 06:27 PM

Thanks. @Nohinn's comment works.
Tags
DropDownList
Asked by
David
Top achievements
Rank 1
Answers by
Nohinn
Top achievements
Rank 1
Share this question
or