RadCombo box how to find selectedIndex

6 posts, 1 answers
  1. SIP IT
    SIP IT avatar
    2 posts
    Member since:
    Oct 2011

    Posted 21 Nov 2011 Link to this post

    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.

  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 21 Nov 2011 Link to this post

    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.
  3. Answer
    Bozhidar
    Admin
    Bozhidar avatar
    1146 posts

    Posted 21 Nov 2011 Link to this post

    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
  4. SIP IT
    SIP IT avatar
    2 posts
    Member since:
    Oct 2011

    Posted 21 Nov 2011 Link to this post

    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>

  5. Arun
    Arun avatar
    2 posts
    Member since:
    Oct 2015

    Posted 19 Aug 2016 Link to this post

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

  6. Vaibhav
    Vaibhav avatar
    1 posts
    Member since:
    Jul 2017

    Posted 11 Sep in reply to SIP IT Link to this post

    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??
Back to Top