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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:16:04+00:00 2026-05-26T21:16:04+00:00

My actual question is quite bit complicated compared to its title. I am very

  • 0

My actual question is quite bit complicated compared to its title. I am very new to Javascript and jQuery so please bear with me.

I would suggest that you run this code first before reading my question so you can understand what I’m trying to do.

<html>
<head>
<script type="text/javascript" src="jquery1.6.4min.js"></script>
<script type="text/javascript" > 
var selectedAddFootballPlayerId = '';
var selectedAddFootballPlayerName = '';
var selectedRemoveFootballPlayerId = '';
var selectedRemoveFootballPlayerName = '';

$(document).ready(function() {
 $('#listboxFootballPlayers option').click(function() {
  selectedAddFootballPlayerId = $(this).attr('value');
  selectedAddFootballPlayerName = $(this).text();
 });

 $('#selectedFootballPlayers option').click(function() {
  selectedRemoveFootballPlayerId = $(this).attr('value');
  selectedRemoveFootballPlayerName = $(this).text();
 });

 $('input#btnAddFootballPlayerToList').click(function() {
  if (selectedAddFootballPlayerId == '') {
   alert("Select a football player to be added from the list.");
  } else {
   var option = new Option(selectedAddFootballPlayerName , selectedAddFootballPlayerId);
   $(option).html(selectedAddFootballPlayerName); 
   $('#selectedFootballPlayers').append(option);
   selectedAddFootballPlayerId = '';
   selectedAddFootballPlayerName = '';
  }
 });

 $('input#btnRemoveFootballPlayerFromList').click(function() {
  if (selectedRemoveFootballPlayerId == '') {
   alert("Select a football player to be removed from the list.");
  } else {
   var option = new Option(selectedRemoveFootballPlayerName , selectedRemoveFootballPlayerId);
   $(option).html(selectedRemoveFootballPlayerName); 
   $('#listboxFootballPlayers').append(option);
   selectedRemoveFootballPlayerId = '';
   selectedRemoveFootballPlayerName = '';
  }
 });
});
</script>
</head>
<body>
<table>
 <tr>
  <td>
   <select id="listboxFootballPlayers" size="5" multiple="multiple" style="width: 200px;">
    <option value="l1">Cristiano Ronaldo</option>
    <option value="l2">Ricardo Kaka</option>
    <option value="l3">Lionel Messi</option>
    <option value="l4">Gerd Muller</option>
    <option value="l5">Johan Crujjf</option>
    <option value="l6">Franz Beckenbauer</option>
    <option value="l7">David Beckham</option>
   </select>
  </td>
                    
  <td>
   <table>
    <tr><td><input type="button" id="btnAddFootballPlayerToList" value="->" /> </td></tr>
    <tr><td><input type="button" id="btnRemoveFootballPlayerFromList" value="<-" /></td></tr>
   </table>
  </td>
                        
  <td>
   <select id="selectedFootballPlayers" size="5" multiple="multiple" style="width: 200px;"></select>
  </td>
 </tr>
 </table>
 </body>
 </html>

Before I start with the question please take note:

#listboxFootballPlayers – The listbox on the left
#selectedFootballPlayers – The listbox on the right

I have 2 questions:

  1. How can I remove the selected item / option from #listboxFootballPlayers after I clicked on -> button.

  2. Why is it that when I click on <- after I selected an item / option from #selectedFootballPlayers it gives me a message Select a football player to be removed from the list. It seems to me that it doesn’t assign the value on the variable selectedRemoveFootballPlayerId.

Please ask me if there are something that are not clear with my question. Please help.

Here is the jsfiddle link: http://jsfiddle.net/7vspM/

  • 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-26T21:16:04+00:00Added an answer on May 26, 2026 at 9:16 pm
    $(document).ready(function() {
        $('#listboxFootballPlayers option').click(function() {
            selectedAddFootballPlayerId = $(this).attr('value');
            selectedAddFootballPlayerName = $(this).text();
        });
        $('#selectedFootballPlayers option').live('click',  // `live()` event for `option` bcoz, option in this select will  
                                                            // create after DOM ready
        function() {
            selectedRemoveFootballPlayerId = $(this).attr('value');
            selectedRemoveFootballPlayerName = $(this).text();
        });
        $('input#btnAddFootballPlayerToList').click(function() {
            if (selectedAddFootballPlayerId == '') {
                alert("Select a football player to be added from the list.");
            } else {
                var option = new Option(selectedAddFootballPlayerName, selectedAddFootballPlayerId);
                $(option).html(selectedAddFootballPlayerName);
                $('#selectedFootballPlayers').append(option);
                $('#listboxFootballPlayers option:selected').remove();  // remove selected option
                selectedAddFootballPlayerId = '';
                selectedAddFootballPlayerName = '';
            }
        });
        $('input#btnRemoveFootballPlayerFromList').click(function() {
            if (selectedRemoveFootballPlayerId == '') {
                alert("Select a football player to be removed from the list.");
            } else {
                var option = new Option(selectedRemoveFootballPlayerName, selectedRemoveFootballPlayerId);
                $(option).html(selectedRemoveFootballPlayerName);
                $('#selectedFootballPlayers option:selected').remove(); // remove selected option
                $('#listboxFootballPlayers').append(option);
                selectedRemoveFootballPlayerId = '';
                selectedRemoveFootballPlayerName = '';
            }
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Bit of a bizarre question, but does anyone know the actual limit to the
I'm not quite sure what the actual behavior is, so my first question is:
Disclaimer: this question is driven by my personal curiosity more than an actual need
I asked a separate question (now deleted) for what I thought the actual problem
This is more of a theoretical question than an actual problem I have. If
I have done impersonation in SharePoint quite a bit in the past by doing
So, I am fairly new to JavaScript coding, though not new to coding in
I've seen very similar questions to this, but I can't quite decide if they
This may not be a actual question, but I just asked it for the
EDIT: This question is quite long, including several examples of the sort of thing

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.