Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Working with dual listbox and jquery

Posted on 2013-01-07
1
Medium Priority
?
1,442 Views
Last Modified: 2013-01-08
I have a set of listboxes that I move items from one to the other. I want to grab all the values from the second listbox "Listbox2" and store the values in a hidden field for postback to the database. I'm getting an error in this line of code saying Options is null.

arr[i] = $("#ListBox2").options[i].value;

Open in new window


Here is my complete code. What am I doing wrong?

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="http://code.jquery.com/jquery-1.8.2.js"></script>
    <script src="http://code.jquery.com/ui/1.8.18/jquery-ui.js"></script>
     <script type="text/javascript">
         $(function() {


             $("#MoveRight,#MoveLeft").click(function (event) {
                 var id = $(event.target).attr("id");
                 var selectFrom = id == "MoveRight" ? "#ListBox1" : "#ListBox2";
                 var moveTo = id == "MoveRight" ? "#ListBox2" : "#ListBox1";

                 var selectedItems = $(selectFrom + " :selected").toArray();
                 $(moveTo).append(selectedItems);                 
                 selectedItems.remove;
                 var arr = [];
                 for (var i = 0; i < $("#ListBox2").length; i++) {
                     arr[i] = $("#ListBox2").options[i].value;
                 }
                 $("#hf1").val(arr.join(','));
             });
         });
    </script>
    
</head>
    <body>
<form runat="server">

    <asp:ListBox ID="ListBox1" runat="server" Height="300px" SelectionMode="Multiple">
    </asp:ListBox>
           
      <input id="MoveRight" type="button" value=" >> " />
      <input id="MoveLeft" type="button" value=" << " />
       
      <asp:ListBox ID="ListBox2" runat="server" Height="300px" SelectionMode="Multiple"></asp:ListBox>  <asp:HiddenField ID="hf1" runat="server" />    
    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
    </form>
</body>
</html>

Open in new window

0
Comment
Question by:NickMalloy
1 Comment
 
LVL 22

Accepted Solution

by:
Mrunal earned 1600 total points
ID: 38753727
Here is your solution:

http://jsfiddle.net/mycbz/3/
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses
Course of the Month20 days, 23 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question