Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET Javascript jQuery , +
Hi Everybody ,
 
I want to findout the list box selected values in asp.net using jquery can any body help?
 
its urgent?
 
Thanks in Advance
Posted 14-Apr-13 20:26pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

check this
 
http://stackoverflow.com/questions/3297174/jquery-any-item-selected-in-listbox[^]
 
http://stackoverflow.com/questions/3113851/selecting-item-from-asp-net-listbox-using-jquery[^]
 

Here is the code
<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>
 

<script type="text/javascript">
 
    $("#<%=ListBox1.ClientID %>").change(function () {
        $("#<%=text1.ClientID %>").val($(this).val());
    });
 

</script>
 

Hope it helps...
 

 

Edited
 

Jquery code
$("#<%=ListBox1.ClientID %>").change(function () {
      var selectedItems = $(this).val();
      var value = 0;
      for (var i = 0; i < selectedItems.length; i++) {
          value = parseInt(value)+ parseInt(selectedItems[i]);
      }
      $("#<%=text1.ClientID %>").val(value);
  });
 
Assuming the listbox is like this
<asp:ListBox ID="ListBox1" runat="server" Width="50px" Height="100px"
         SelectionMode="Multiple">
         <asp:ListItem Value="1">1</asp:ListItem>
         <asp:ListItem Value="2">2</asp:ListItem>
         <asp:ListItem Value="3">3</asp:ListItem>
         <asp:ListItem Value="4">4</asp:ListItem>
         <asp:ListItem Value="5">5</asp:ListItem>
     </asp:ListBox>
  Permalink  
v3
Comments
Member 9474372 at 15-Apr-13 1:51am
   
i want to display all the selected values of the selected item in listbox
 
(i need to sum all the selected values and i need to store in a variable)
tuananh301191 at 22-Sep-13 0:01am
   
Thanks, it works perfectly for me.
 
Best Regards
Naz_Firdouse at 15-Apr-13 3:03am
   
Here is the Jquery to sum all the selected values
$("#<%=ListBox1.ClientID %>").change(function () {
var selectedItems = $(this).val();
var value = 0;
for (var i = 0; i < selectedItems.length; i++) {
value = parseInt(value)+ parseInt(selectedItems[i]);
}
$("#<%=text1.ClientID %>").val(value);
});
 
Assuming your listbox items are like this
 
<asp:ListItem Value="1">1
<asp:ListItem Value="2">2
<asp:ListItem Value="3">3
<asp:ListItem Value="4">4
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hello
Check out this sample
http://jsfiddle.net/JthcU/[^]
 
You will get comma separated string of selected items.
If nothing is selected then it will return null.
  Permalink  
Comments
Member 9474372 at 15-Apr-13 1:38am
   
i want to display all the selected values of the selected item in listbox
 
(i need to sum all the selected values and i need to store in a variable)
patel_vijay at 15-Apr-13 1:53am
   
http://jsfiddle.net/JthcU/1/
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

$('#Select1').click(function(e) {
  $("#Select1").value());
});
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Try this:
var sum = 0;
$('.ListBox1 option').each(function(index) {
  if ( ($(this).is(':selected')) {
    // do stuff if selected
    sum += this.value;
  }
  else {
   // this one isn't selected, do other stuff
  }
});
Also check HOW TO GET LISTBOX ID & SELECTED VALUE using jquery[^].
 

--Amit
  Permalink  
v2
Comments
Member 9474372 at 15-Apr-13 1:51am
   
i want to display all the selected values of the selected item in listbox
 
(i need to sum all the selected values and i need to store in a variable)
Member 10232382 at 11-Sep-13 5:18am
   
how to check whether the listbox item is selected or not
_Amy at 15-Apr-13 1:56am
   
Take a variable and do the operation. See my modified codes..
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

$("#aSelect").click(function(){
    var selectedValues = [];
    $("#lstSelect :selected").each(function(){
        selectedValues.push($(this).val());
    });
    alert(selectedValues);
    return false;
});
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 5,170
1 DamithSL 4,357
2 Maciej Los 3,750
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,851


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 15 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100