This is a migrated thread and some comments may be shown as answers.

RadCombo box how to find selectedIndex

5 Answers 883 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
SIP IT
Top achievements
Rank 1
SIP IT asked on 21 Nov 2011, 12:00 PM

Hi Telerik,
I tried to find the text from combobox doesn't work using the normal dropdownlist javascript as below

<telerik:RadScriptBlock ID="rsbAction" runat="server">

    <script type="text/javascript">
        function action(sender, args) {
            var rcbCategory = document.getElementById('<%=rcbCategory.ClientID %>');
            var result = false;

            if (rcbCategory != null)
                if (rcbCategory.selectedIndex > 0)
                    result = true;
            args.IsValid = result;
        }

    </script>

</telerik:RadScriptBlock>
I as well tried the code provided by the sample also doesn't work as below :

<telerik:RadScriptBlock ID="RadScriptBlock1" runat="server">
    <script type="text/javascript">
        function category(sender, args) {
            var result = false;
            var rcbCategory = $find("<%=rcbCategory.ClientID %>");
            var rcbCategoryText = rcbCategory.get_text();

            if (rcbCategoryText.Length > 1) {
                var node = rcbCategory.findItemByText(rcbCategoryText);
                if (node) {
                    var value = node.get_value();
                    if (value.length > 0 && value % 2 == 0)
                        result = true;
                }
            }
            args.IsValid = result;
        }
    </script>

</telerik:RadScriptBlock>

Thank you.

5 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 21 Nov 2011, 01:19 PM
Hello,

You can attach the client event OnClientSelectedIndexChanged and access the Text value.
JS:
<script type="text/javascript">
function OnClientSelectedIndexChanged(sender, args)
 {
    alert(args.get_item().get_text());
 }
</script>

Also check the following help documentation.
Client-Side Basics

-Shinu.
0
Accepted
Bozhidar
Telerik team
answered on 21 Nov 2011, 05:12 PM
Hello,

You can also access the selected item from an arbitrary function. Here's a sample code, where the selected item text is alerted whenever the user clicks a button:

<form id="form1" runat="server">
 
<telerik:RadScriptBlock ID="RadScriptBlock1" runat="server">
    <script type="text/javascript">
        function clientFunction() {            
            var combo = $find("<%= RadComboBox1.ClientID %>");
            alert("Index: " + combo.get_selectedIndex() + ", Text: " + combo.get_selectedItem().get_text());
        }
    </script>
</telerik:RadScriptBlock>
 
<div>
    <asp:ScriptManager ID="ScriptManager1" runat="server" />
    <telerik:RadComboBox ID="RadComboBox1" runat="server">
        <Items>
            <telerik:RadComboBoxItem Text="item one" />
            <telerik:RadComboBoxItem Text="item two" />
            <telerik:RadComboBoxItem Text="item three" />
            <telerik:RadComboBoxItem Text="item four" />
            <telerik:RadComboBoxItem Text="item five" />
            <telerik:RadComboBoxItem Text="item six" />
            <telerik:RadComboBoxItem Text="item seven" />
        </Items>
    </telerik:RadComboBox>
    <br />
    <asp:Button ID="Button1" runat="server" OnClientClick="clientFunction()" Text="Button" />
</div>
</form>
  
Kind regards,
Bozhidar
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
SIP IT
Top achievements
Rank 1
answered on 22 Nov 2011, 02:37 AM

Hi Bozhidar,

Thank for you help for on getting the selectedIndex my custom validator work using this script

<telerik:RadScriptBlock ID="rsbCategory" runat="server">
    <script type="text/javascript">
        function category(sender, args) {
            var result = false;
            var rcbCategory = $find("<%=rcbCategory.ClientID %>");
            var rcbCategoryIndex = rcbCategory.get_selectedIndex();
  
            if (rcbCategoryIndex > 0) 
                result = true;
  
            args.IsValid = result;
        }
    </script>
</telerik:RadScriptBlock>

0
Arun
Top achievements
Rank 1
answered on 19 Aug 2016, 06:10 PM

this worked for me: $find("<%= dropdownId.ClientID %>").get_selectedItem().get_index()

0
Vaibhav
Top achievements
Rank 1
answered on 11 Sep 2017, 11:47 AM
I am using checkbox =true in my radcombobox,I want to get checked item index in javascript.I am using OnClientItemChecked="OnClientItemChecked"  can anyone  help me??
Tags
ComboBox
Asked by
SIP IT
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Bozhidar
Telerik team
SIP IT
Top achievements
Rank 1
Arun
Top achievements
Rank 1
Vaibhav
Top achievements
Rank 1
Share this question
or