how to get if combobox selected item is null with javascript?

7 posts, 1 answers
  1. Amit
    Amit avatar
    88 posts
    Member since:
    Sep 2010

    Posted 09 Nov 2010 Link to this post

    hey everyone,

    I want to validate a button such that if combobox selected items count is equal to zero then button click will show an alert on client side. I am doing this:
    function filterSearch()
    {
      var combo = $find('<%=RadComboBox1.ClientID %>');
      var textbox = document.getElementById("txtSearch");
      var selectedItemsCount = combo.get_selectedItems().length;
      if (selectedItemsCount == 0)
      {
        alert("You must select a category before processing search!");
        return false;
      }
    }
    This ain't working though. This is my button:
    <asp:Button ID="btnSearch" runat="server" Style="float: left; color: black; font-weight: bold" Text="Search" OnClick="btnSearch_Click" OnClientClick="javascript: return filterSearch();" />
    How can I do this?

    Thanks,
    Amit

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

    Posted 09 Nov 2010 Link to this post

    Hello Amit,

     Here is code snippet I tried for my application for a similar scenario. I hope this would help you also. 
    Here is the code.

    Javascript:
    function filterSearch()
    {
           var combo = $find('<%=RadComboBox1.ClientID %>');
           var textbox = document.getElementById("txtSearch");
           if (combo.get_selectedIndex() == null)
           {
     
               alert("You must select a category before processing search!");
               return false;
           }
    }

    Shinu.
  3. Amit
    Amit avatar
    88 posts
    Member since:
    Sep 2010

    Posted 09 Nov 2010 Link to this post

    Thanks Shinu,that was perfect...
  4. David
    David avatar
    291 posts
    Member since:
    Mar 2014

    Posted 03 May 2018 Link to this post

    To resurrect old thread:

    if (combo.get_selectedIndex() == null)

    gives me an error in Firefox. I tried other solutions with no luck

  5. Marin Bratanov
    Admin
    Marin Bratanov avatar
    5128 posts

    Posted 08 May 2018 Link to this post

    Hi David,

    What is the error and its stack trace? I am attaching here a video of my test and this seems to work as expected. What am I missing from your setup and attempts? Can you paste here the combo box markup so I can see how you are using it?


    Regards,
    Marin Bratanov
    Progress Telerik
    Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  6. David
    David avatar
    291 posts
    Member since:
    Mar 2014

    Posted 08 May 2018 in reply to Marin Bratanov Link to this post

    Hi Marin,

        Embarrassingly, it works fine now, not sure what happened, likely my bad.

        Sorry to resurrect the issue.

    Thank you

    David

  7. Marin Bratanov
    Admin
    Marin Bratanov avatar
    5128 posts

    Posted 08 May 2018 Link to this post

    No problem, David, better safe than sorry.

    --Marin

Back to Top