Solved

Help on Jquery/Select Option

Posted on 2012-03-21
6
272 Views
Last Modified: 2012-06-21
I need help on this.

I have 2 select options and the users can transfer messages from one box to another. I need to

Prevent User to transfer  - "Special Message*" (splMsg) to "Available Fields"
Prevent User to mix and combine all messages with "Special Message" under "Selected Fields" to "Available Fields" (user can combine all messages on "Selected Field" and can transfer to "Available Fields" but cannot move "Special Message"
      

Code is given below. Ignore my styles for now.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <style>
        .row
        {
            width: 800px;
            background-color: Gray;
        }      
        .fieldsTable
        {
            float: left;
            width: 200px;            
        }
         .fieldsTableArrows
        {
            float: left;
            width: 100px;            
        }
    </style>
</head>
<body>
    <form>
    <div class="row">
       
        <div class="fieldsTable">
            <label>Available Fields:</label><br />
            <select id="availMsg" name="availMsg"  multiple="multiple" class="transferList"  size="5">
                <option value="msg01">Left Message 01</option>
                <option value="msg02">Left Message 02</option>
                <option value="msg03">Left Message 03</option>
                <option value="msg04">Left Message 04</option>
                <option value="msg05">Left Message 05</option>
            </select>
        </div>
        <div class="fieldsTableArrows">
            <input type="button" id="moveLeft" value="&gt;&gt;">
            <input type="button" class="moveRight" id="moveRight" value="&lt;&lt;">
        </div>
        <div class="fieldsTable">
            <label>Selected Fields:</label><br />
            <select id="selMsg" name="selectedFields" class="transferList" multiple="multiple"  size="5">
                <option value="msg11">Right Message 01</option>
                <option value="msg12">Right Message 02</option>
                <option value="msg13">Right Message 03</option>
                <option value="msg14">Right Message 04</option>
                <option value="splMsg">Special Message*</option>
            </select>
        </div>
    </div>
    </form>
</body>
</html>

<script type="text/javascript" language="javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js"></script>

<script type="text/javascript" language="javascript">
    $(function() {
        $('#moveLeft').click(function() {
            return !$('#availMsg option:selected').remove().appendTo('#selMsg').blur();
        });
        $('#moveRight').click(function() {
            $('#selMsg option:selected').remove().appendTo('#availMsg');            
            $('#selMsg').blur();
            return false;
        });
    });
</script>
0
Comment
Question by:mahaq
  • 3
  • 3
6 Comments
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
http://api.jquery.com/filter/

for example
$('#moveRight').click(function() {
  $('#selMsg option:selected').filter(function(index) {
      return $(this).val() != "splMsg";
   }).remove().appendTo('#availMsg');            
  $('#selMsg').blur();
  return false;
 });

Open in new window

0
 

Author Comment

by:mahaq
Comment Utility
Dear "mplungjan",

Many thanks on this quick reply and possibly the solution. I need little help on alerting the user not to mix and match the "splMsg" with all other messages while transfering from Right to Left. Also I need another alert when user tries to transfer "splMsg" too. Well I just need to alert & later on use a message key.

If you can provide a solution on these, I shall accept it in complete. Thanks in advance.
0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 500 total points
Comment Utility
You mean
$('#moveRight').click(function() {
  $('#selMsg option:selected').filter(function(index) {
      if ($(this).val() == "splMsg") {
        alert('Please do not transfer '+$(this).text());
        return false;
      }
      return $(this).val() != "splMsg";
   }).remove().appendTo('#availMsg');            
  $('#selMsg').blur();
  return false;
 });

Open in new window

0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Closing Comment

by:mahaq
Comment Utility
Dear "mplungjan",

Many thanks for helping me out on this.
I am bothering you one last time, can you please give me an alternate way, when users select  combination of any or all messages with splMsg, then transfer be not performed and force user to only select other than splMsg. I mean I need another alert (telling users to deselect splMsg and try again), but no change to your current logic, becoz it works for me.

Thank you so much.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
Sounds very complicated to have an option not selectable
What exactly do you gave that makes you want to force these issues in the user?
0
 

Author Comment

by:mahaq
Comment Utility
Never mind, I'll stick to the last solution. Thanks again.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now