When I run the following JS I always get the confirm box popping up despite the list visibly having items.
<asp:ListBox ID="list" runat="server" Width="135px" Rows="8" />
function CheckListEmpty() {
if ($("#list").length == 0) {
if (confirm("Are you sure?")) {
//they clicked OK so save and close
return true;
}
else {
//do nothing they hit cancel.
return false;
}
}
else
return true;
}
What am I doing wrong?
$("#list").lengthreturns the number of elements matched by the jQuery selector.It isn’t matching anything because you aren’t using the ListBox’s
ClientId; you need to change it to$("#<%= list.ClientId %>").To get the number of items in the
<select>, you need to write$("#<%= list.ClientId %> option").length