1

I am just trying to check the amount of items in my ASP ListBox.

If the item count is greater than 10, then I remove the item at the 11th element.

It works the first time, but afterwards, only the item count correctly executes.

The lstEms.remove(10) no longer removes the item at the eleventh element.

I tried to null lstEms at the end of the function but that didn't do anything.

function countselected() {
    var lstEms = document.getElementById("<%= lstBox.ClientID %>");
    var selOpts = $("#ASP_lstBox")[0].length;
    console.log(selOpts);
    if (selOpts > 10) {
        alert("Cannot add more than 10 recipients");
        lstEms.remove(10);
        var newOpts = $("#ASP_lstBox")[0].length;
        alert(newOpts);
    };
};

Let me know if you have any suggestions. Thanks in advance.

UPDATE:

<asp:ListBox ID="lstBox" runat="server" Height="90px" Width="219px"></asp:ListBox> 

UPDATE 2:

Here is my CodeBehind that is calling the script:

protected void btn_AddItems_Click(object sender, EventArgs e)
    {
        lstBox.Items.Add(item);
        ScriptManager.RegisterStartupScript(Page, typeof(Page), "", "countselected()", true); 
    }
15
  • The id is unique on your html page?
    – R3tep
    Jun 12, 2015 at 15:14
  • yeah, i just replaced it with a generic ID, but it is unique @R3tep
    – terbubbs
    Jun 12, 2015 at 15:17
  • maybe post a fiddle to help demonstrate?
    – blink-fish
    Jun 12, 2015 at 15:21
  • @blink-fish tbh, i have yet to test any js out on fiddle.. but from Firebug and my asp project, after the 2nd countselected() call, it still gives the correct item count @ alert(newOpts) but lstEms.remove(10) does not remove an item.
    – terbubbs
    Jun 12, 2015 at 15:25
  • You are making a check against different element and removing item from different element. That's not good practice. Jun 12, 2015 at 15:29

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.