maztt
maztt

Reputation: 12294

jquery Any item selected in listbox

how can i check with jquery that an item is selected or not in listbox?

Upvotes: 3

Views: 6470

Answers (2)

Amr Badawy
Amr Badawy

Reputation: 7673

check the following example that change textbox with value clicked in asp listbox

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <script type="text/javascript" language="javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ListBox ID="ListBox1" runat="server" Width="50px" Height="100px" 
            SelectionMode="Multiple">
            <asp:ListItem Value="One">1</asp:ListItem>
            <asp:ListItem Value="Two">2</asp:ListItem>
            <asp:ListItem Value="Third">3</asp:ListItem>
            <asp:ListItem Value="Four">4</asp:ListItem>
            <asp:ListItem Value="Five">5</asp:ListItem>
        </asp:ListBox>
        <br />
        <asp:TextBox ID="text1" runat="server"></asp:TextBox>
    </div>
    </form>
</body>
</html>

<script type="text/javascript">
    $('#ListBox1').click(function() {
        $("input#text1").val($("#ListBox1 option:selected").val());
    }); 
</script>

Upvotes: 3

Sachin R
Sachin R

Reputation: 11876

The :selected selector works for elements. It does not work for checkboxes or radio inputs; use :checked for them.

http://api.jquery.com/selected-selector/

Upvotes: 1

Related Questions