/    Sign up×
Community /Pin to ProfileBookmark

Function problem…

Hi all. Having a problem with a function. I have 2 multi select boxes side by side in a form. The idea is that a single click on an item in box A will move said item to box B. A click on an item in box B will move the item to box A.

At the moment, I can get items from the left list to the right list, but not from right to left. If someone can eyeball this function and let me know what’s wrong, I’d appreciate it. Thanks in advance!

<SCRIPT LANGUAGE=”JavaScript”>
<!– Begin
function moveOver(fromList,toList,limit)
{
var undefined;
var boxLength = toList.length;
var selectedItem = fromList.selectedIndex;
var selectedText = fromList.options[selectedItem].text;
var selectedValue = fromList.options[selectedItem].value;

var i;
var isNew = true;;

if ( ((limit != undefined) && (boxLength<limit)) || limit == undefined ) {

if (boxLength != 0) {
for (i = 0; i < boxLength; i++) {
thisitem = toList.options[i].text;
if (thisitem == selectedText) {
isNew = false;
break;
}
}
}

if (isNew) {
newoption = new Option(selectedText, selectedValue, false, false);
toList.options[boxLength] = newoption;
}

fromList.selectedIndex=-1;

} else {
alert(“This is a limit of ” + limit + ” choices for this list.”);
}

}
</SCRIPT>

<form>
All Available Users:
<select multiple name=”userID” style=”width:275;” size=”6″ onchange=”moveOver(this,this.form.list);”>
#WHOLE BUNCH OF OPTION TAGS HERE TO POPULATE THE LIST#
</select>

Users in this Group:
<select multiple name=”list” style=”width:275;” size=”6″ onchange=”moveOver(this,this.form.userID);”>
#WHOLE BUNCH OF OPTION TAGS HERE TO POPULATE THE LIST#
</select>
</form>

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@theBody44May 13.2004 — Are you sure?? I took your code, added a couple options on both sides and have had no problem moving anything left or right. Of course, since you code it so that an item can only appear once, if you add it to the right from the left, you can't add it back to the left. From what I see, it works perfectly.
Copy linkTweet thisAlerts:
@NanwedarauthorMay 13.2004 — Ahh yes, thx. It works as it is coded to work. Unfortunately, this doesn't do what I need.

I'm trying to build a user manager page that assigns users to a group.

Select Box A (on left side) is going to be filled with a whole bunch of names (users pulled from a database). This box represents all available users.

Select Box B (or right side) represents users in a given group. This may be populated or empty when page loads.

I need to accomplish the following:

1- Click a user on left side moves user to right side, user name disappears from left side box.

2- Click a user in right side box moves user to left side box, user name disappears from right side.

3- Basically, a user can only be in one box or the other, not both, and can be moved right to left or left to right.

Anyone have any clue how to accomplish this or know of any links I could look at that use this sort of functionality? Thanks in advance!
Copy linkTweet thisAlerts:
@theBody44May 13.2004 — [CODE]fromList.options[selectedItem] = null;[/CODE]

put this right before the line[CODE]fromList.selectedIndex=-1;[/CODE]in your moveOver function.?
Copy linkTweet thisAlerts:
@NanwedarauthorMay 13.2004 — SWEET MOTHER OF MEAT!

You rock man. Thanks!!


EDIT---

I hate to push my luck on this, but is there an easy way to make these lists alphabetically ordered? Such that the lists resort when items are moved back and forth?
×

Success!

Help @Nanwedar spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...