locked
Jquery class selector instead of id selector for select option RRS feed

  • Question

  • User1487175000 posted

    Hi,

    I am trying to loop through my select list to store keys and values. But i want to select the selectlist with class selector instead of id selector.

    <asp:ListBox ID="listOfOpenBills" runat="server" Width="95%" Height="100px" CssClass="openBill"></asp:ListBox>
    
    function loadKeys() {
    var Keys = [];
    var Values = [];
    $.each($("#<% = listOfOpenBills.ClientID %> option"), function (index, item) {
        Keys.push(item.value);
        Values.push(item.innerHTML);
    });
    }

    Friday, July 5, 2019 11:31 AM

Answers

  • User475983607 posted

    Yes i also want to get options in the listbox as well. How i can do that?

    /Shahid

    Add "option" to the selector.

        <asp:ListBox ID="listOfOpenBills" runat="server" Width="95%" Height="100px" CssClass="openBill">
            <asp:ListItem Value="1">1</asp:ListItem>
            <asp:ListItem Value="2">2</asp:ListItem>
            <asp:ListItem Value="3">3</asp:ListItem>
        </asp:ListBox>
    
        <script>
            $.each($(".openBill > option"), function (index, item) {
                console.log(item);
            });
        </script>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 5, 2019 1:43 PM

All replies

  • User475983607 posted

    Then use a class selector...

    $(".openBill")

    The jQuery docs cover selectors quite well.

    https://api.jquery.com/category/selectors/

    Friday, July 5, 2019 1:21 PM
  • User1487175000 posted

    Yes i also want to get options in the listbox as well. How i can do that?

    /Shahid

    Friday, July 5, 2019 1:22 PM
  • User475983607 posted

    Yes i also want to get options in the listbox as well. How i can do that?

    /Shahid

    Add "option" to the selector.

        <asp:ListBox ID="listOfOpenBills" runat="server" Width="95%" Height="100px" CssClass="openBill">
            <asp:ListItem Value="1">1</asp:ListItem>
            <asp:ListItem Value="2">2</asp:ListItem>
            <asp:ListItem Value="3">3</asp:ListItem>
        </asp:ListBox>
    
        <script>
            $.each($(".openBill > option"), function (index, item) {
                console.log(item);
            });
        </script>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 5, 2019 1:43 PM