First let's correct the getElementID
in your code to getElementById
:
OnClientClick="alert(document.getElementById('<%=ListBox1.ClientID %>').options.length);"
If you want to know which item is selected, use the selectedIndex
property:
OnClientClick="alert(document.getElementById('<%=ListBox1.ClientID %>').selectedIndex);"
If you want the value of the option rather than the index, use the options collection with the index:
OnClientClick="var s=document.getElementById('<%=ListBox1.ClientID %>');alert(s.options[s.selectedIndex].value);"
Edit:
This would work if the control where you are trying to use it was not a server control, for example:
<input type="button" onclick="alert(document.getElementById('<%=ListBox1.ClientID %>').options.length);" />
As you have a server control, you can't use a script tag (<%= %>) inside the control. You have to set the property from code behind:
TheButton.OnClientClick = "alert(document.getElementById('" + ListBox1.ClientID + "').options.length);";