Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All,
New to client end programming and i was hoping someone could help on how i would go about in achieving this.

I have the following below in my project

Markup code
ASP.NET
<form id="form1" runat="server">
    <div>
    <asp:ListBox runat="server" ID="MultiSelect" AutoPostBack="true" SelectionMode="multiple" OnSelectedIndexChanged="MultiSelect_SelectedIndexChanged">
                               
                            
    </div>
        <div>
            <p>The Selected Value is:</p><asp:TextBox  ID="listBoxValue" runat="server" >
        </div>
    </form>


code behind:

C#
public partial class list_show : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                MultiSelect.Items.Add(new ListItem { Text = "John Doe", Value = "1" });
 
                MultiSelect.Items.Add(new ListItem { Text = "Jane Doe", Value = "2" });
                
            } 
        }
 
        protected void MultiSelect_SelectedIndexChanged(object sender, EventArgs e)
        {
            listBoxValue.Text = MultiSelect.SelectedItem.Text;
        }
    }


How can i go about in achieving the same thing but using javascript/jquery?

What I have tried:

I have written the code in backend but i just need to do a similar thing so i have been reading about jquery and javascript
Posted
Updated 21-Nov-17 3:52am

<form id="form1" runat="server">
    <div>
    <asp:ListBox runat="server" ID="MultiSelect" AutoPostBack="false" SelectionMode="multiple" OnSelectedIndexChanged="MultiSelect_SelectedIndexChanged">
    </asp:ListBox>   
                            
    </div>
    <div>
        <p>The Selected Value is:</p><asp:TextBox  ID="listBoxValue" runat="server" />
    </div>
</form>
    
<script>
    $(document).ready(function () {
        var select = $("#<%=MultiSelect.ClientID %>");
        var txt = $("#<%=listBoxValue.ClientID %>");

        select.change(function (e) {
            var result = "";
            var options = select.children("option:selected");
            $.each(options, function (i, el) {
                result += $(el).text();
                if (i < options.length - 1) {
                    result += ", ";
                }
            });
               
            txt.val(result);
        });
    });
</script>
 
Share this answer
 
Comments
1Future 21-Nov-17 10:06am    
hi Thanks for this .. it selects the text but how do i select the value and not he text?
F-ES Sitecore 21-Nov-17 10:20am    
use "val" instead of text

result += $(el).val();
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
   
    <script>

        $(function () {
            $('#MultiSelect').on('change', function (a, b, c) {
                var options = a.target.selectedOptions;  // get the selected items
                var temp = []; // temp array to store the selected ids
                for (var i = 0; i < options.length; i++) {
                    var item = options[i]; 
                    var value = item.value;  // id
                    var text = item.text;  // text 
                    temp.push(value);  // add the id to the array

                }
                var selectedValuesCSV = temp.join(','); // all selected values (ID)
                $('#listBoxValue').val(selectedValuesCSV)  // set it to the textbox
            });
        });
    </script>
</head>
<body>
  <form id="form1" runat="server">
    <div>
    <asp:ListBox runat="server" ID="MultiSelect" AutoPostBack="false"  SelectionMode="multiple" ></asp:ListBox>
                               
                            
    </div>
        <div>
            <p>The Selected Value is:</p><asp:TextBox  ID="listBoxValue" runat="server" ></asp:TextBox>
        </div>
    </form>
</body>
</html>
 
Share this answer
 
v2
Comments
Sinisa Hajnal 21-Nov-17 11:58am    
This one wouldn't work because ID will get mangled by the server.
Karthik_Mahalingam 21-Nov-17 12:08pm    
Yes correct.
Refer solution 1 for getting the client ID.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900