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 650565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:04:01+00:00 2026-05-13T22:04:01+00:00

I’m trying to move elements between two lists. Elements when clicked should move to

  • 0

I’m trying to move elements between two lists. Elements when clicked should move to the other list. Prototype JS code:

document.observe('dom:loaded', function() {

  $$('li.available-item').each(function(element){
    element.observe('click', function(){
      element.removeClassName('available-item');
      element.addClassName('selected-item');
      $('selected-list').insert(element);
    });
  });

  $$('li.selected-item').each(function(element){
    element.observe('click', function(){
      element.removeClassName('selected-item');
      element.addClassName('available-item');
      $('available-list').insert(element);
    });
  });

});

Sample elements:

<ul id="available-list">
  <li class="available-item">Apple</li>
  <li class="available-item">Orange</li>
  <li class="available-item">Grapes</li>
</ul>

<ul id="selected-list">
  <li class="selected-item">Pineapple</li>
  <li class="selected-item">Papaya</li>
</ul>

While it’s working the first time I’m clicking on the elements, once an element moves to the other list they don’t move to the other (original) list when clicked.

What am I doing wrong here?

  • 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-05-13T22:04:02+00:00Added an answer on May 13, 2026 at 10:04 pm

    The reason is that when you move an item, it’s still got the old event handler attached that will just move it again.

    This is a classic place for using event delegation. Instead of watching for clicks on the elements, look for clicks on the lists (since clicks bubble up) and then move the relevant element. Something like this:

    $('available-list').observe('click', function(event) {
        var li;
    
        li = event.findElement('li');
        if (li) {
            event.stop();
            li.addClassName('selected-item').removeClassName('available-item');
            $('selected-list').insert(li);
        }
    });
    

    …and the converse for selected-list.

    You could even use just one handler for both lists:

    $('available-list').observe('click', listClick);
    $('selected-list').observe('click', listClick);
    
    function listClick(event) {
        var fromType, toType, li;
    
        // Get the clicked list item
        li = event.findElement('li');
        if (li) {
            // We're handling it
            event.stop();
    
            // Determine whether moving to the selected or available list
            if (this.id.startsWith("selected")) {
                fromType = "selected";
                totype   = "available";
            }
            else {
                fromType = "available";
                totype   = "selected";
            }
    
            // Update class names
            li.addClassName(toType + '-item').removeClassName(fromType + '-item');
            $(toType + '-list').insert(li);
        }
    });
    

    It gets even simpler if you ditch the classes on the items (see below):

    $('available-list').observe('click', listClick);
    $('selected-list').observe('click', listClick);
    
    function listClick(event) {
        var targetList, li;
    
        // Get the clicked list item
        li = event.findElement('li');
        if (li) {
            event.stop();
            targetList = this.id.startsWith("selected") ? "available-list" : "selected-list";
            $(targetList).insert(li);
        }
    });
    

    Somewhat OT, but you may not need those selected-item and available-item classes at all. With the above, you don’t need them to find them anymore, and in your CSS, you can use descendant selectors for styling the elements:

    #selected-list li {
        /* ...styles for the `li` elements in the `selected-list` ... */
    }
    #available-list li {
        /* ...styles for the `li` elements in the `available-list` ... */
    }
    

    If you only want to affect lis that are direct children of the lists, use child selectors instead of descendant selectors (note the >):

    #selected-list > li {
        /* ...styles for the `li` elements in the `selected-list` ... */
    }
    #available-list > li {
        /* ...styles for the `li` elements in the `available-list` ... */
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
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 am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka

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.