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

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

6 Answers 763 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Amit
Top achievements
Rank 1
Amit asked on 09 Nov 2010, 10:16 AM
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

6 Answers, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 09 Nov 2010, 02:03 PM
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.
0
Amit
Top achievements
Rank 1
answered on 10 Nov 2010, 05:23 AM
Thanks Shinu,that was perfect...
0
David
Top achievements
Rank 1
Iron
Iron
Veteran
answered on 03 May 2018, 03:52 PM

To resurrect old thread:

if (combo.get_selectedIndex() == null)

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

0
Marin Bratanov
Telerik team
answered on 08 May 2018, 08:59 AM
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.
0
David
Top achievements
Rank 1
Iron
Iron
Veteran
answered on 08 May 2018, 12:37 PM

Hi Marin,

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

    Sorry to resurrect the issue.

Thank you

David

0
Marin Bratanov
Telerik team
answered on 08 May 2018, 12:59 PM
No problem, David, better safe than sorry.

--Marin

Tags
ComboBox
Asked by
Amit
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Amit
Top achievements
Rank 1
David
Top achievements
Rank 1
Iron
Iron
Veteran
Marin Bratanov
Telerik team
Share this question
or