Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8939373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:48:36+00:00 2026-06-15T10:48:36+00:00

I’m having two list boxes on my page, & I need to move items

  • 0

I’m having two list boxes on my page, & I need to move items between them with the help of javascript.
This is my markup:

<asp:ListBox ID="ListBox1" Height="300px" runat="server" AppendDataBoundItems="true" 
        SelectionMode="Multiple"></asp:ListBox>

<div>

<asp:ImageButton ID="ButtonRight" runat="server" ImageUrl="~/Images/right.gif" OnClientClick="return     
      LeftToRightMoveItems('AddSetup');" /><br />
<br />
<asp:ImageButton ID="ButtonLeft" runat="server" ImageUrl="~/Images/left.gif" OnClientClick="return 
       RightToLeftMoveItems('AddSetup');" />
</div>

<asp:ListBox ID="ListBox2" Height="300px" runat="server" AppendDataBoundItems="true"  
      SelectionMode="Multiple"></asp:ListBox>

Here’s my javascript code:

Javascript:

 function LeftToRightMoveItems() {
        try {
            if (status == "AddSetup") {
                var varFromBox = document.getElementById("<%=ListBox1.ClientID%>").options;
                var varToBox = document.getElementById("<%=ListBox2.ClientID%>").options;
            }

            alert(varFromBox.length);
            alert(varToBox.length);

            if ((varFromBox != null)) {
                if (varFromBox.length < 1) {
                    alert('There are no items to move!');
                    return false;
                }
                if (varFromBox.options.selectedIndex == -1) // when no Item is selected the index will be -1
                {
                    alert('Please select an item to move!');
                    return false;
                }
                while (varFromBox.options.selectedIndex >= 0) {
                    var newOption = new Option(); // Create a new instance of ListItem 
                    newOption.text = varFromBox.options[varFromBox.options.selectedIndex].text;
                    newOption.value = varFromBox.options[varFromBox.options.selectedIndex].value;
                    varToBox.options[varToBox.length] = newOption; //Append the item in Target Listbox
                    varFromBox.remove(varFromBox.options.selectedIndex); //Remove the item from Source Listbox
                }
            }
        }
        catch (e) {
            alert("Following error occured : \n" + e.description);
        }
        return false;
    }

On page load, I’m filling items in ListBox1. But on alert() I’m getting 0 items.

HTML looks like:

<SELECT style="HEIGHT: 300px" id=ListBox1 multiple size=4 name=ctl00$ContentPlaceHolderNewSys$TabContainerMain$tabPanelAdd$tabContainerInnerAdd$tabPanelAdd_1$ListBoxAll1> <OPTION value=1>param1</OPTION> <OPTION value=2>param2</OPTION> <OPTION value=3>param3</OPTION></SELECT> 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T10:48:38+00:00Added an answer on June 15, 2026 at 10:48 am

    The function does not accept a status parameter. Modify the signature of the function to accept status as a parameter.

    function LeftToRightMoveItems(status) {
    ...
    }
    

    There were also some issues with the code once the status was provided. Mainly there were occurrences where the varFromBox was used as a select when it actually represented an array of options.

    For example, varFromBox is initially declared as an array of objects:

    var varFromBox = document.getElementById("<%=ListBox1.ClientID%>").options;
    

    And then in different places the code attempts to access the options property, as if varFromBox were a select element. In reality its actually looking for the options.options

     while (varFromBox.options.selectedIndex >= 0) {..}
    

    Here is what I came up with to negate these issues. I removed the try/catch so any errors were more obvious. Also checkout this example: http://jsfiddle.net/7dVyq/

    function LeftToRightMoveItems(status){
    
                    if (status == "AddSetup") {
                        var varFromBox = document.getElementById("ListBox1").options;
                        var varToBox = document.getElementById("ListBox2");
                    }
    
                    alert(varFromBox.length);
                    alert(varToBox.length);
    
                    if ((varFromBox != null)) {
                        if (varFromBox.length < 1) {
                            alert('There are no items to move!');
                            return false;
                        }
                        console.log(varFromBox.selectedIndex);
                        if (varFromBox.selectedIndex == -1) // when no Item is selected the index will be -1
                        {
                            alert('Please select an item to move!');
                            return false;
                        }
                        for (var i = 0; i < varFromBox.length; i++) {
                            if (varFromBox[i].selectedIndex != -1) {
                                var newOption = new Option(); // Create a new instance of ListItem
                                newOption.text = varFromBox[i].text;
                                newOption.value = varFromBox[i].value;
                                varToBox.options[varToBox.length] = newOption; //Append the item in Target Listbox
                                document.getElementById("ListBox1").remove(varFromBox[i]); //Remove the item from Source Listbox
                            }
                        }
                        return false;
                    }
                }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the
I used javascript for loading a picture on my website depending on which small

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.