473,387 Members | 3,750 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Multiple Selection in javascript

I have a function like the one below to move a selected option from
one listbox to another. But I want to be able to select multiple and
be able to move those selections to the other listbox. Can anyone help
me on that.
Thank you

<html>
<head>
<script language="JavaScript">
function addOption(sBox, aBox) {

//onClick="addOption(document.forms[0].name+'.selected_us',
document.forms[0].name+'.available_us');">
//onClick="outputSelected(document.forms[0].select.options)">
var checkFlag=true;
var dummVal;
var pElem;
var pOptionVal;
var pOptionText;
pElem = eval("document."+sBox);
pOptionVal = eval("document."+aBox+".value");
//alert("selected items:\n" + pOptionVal);
if (pOptionVal != ""){
pOptionText = eval("document."+aBox+"[document."+aBox+".selectedIndex].text");
for (x = 0; x < pElem.length; x++) {
dummVal= pElem[x].value;
if ( pOptionVal == dummVal){
checkFlag=false;
}
}
if (checkFlag){
if (pElem.length==1){
if (pElem[0].value =="NONE"){
pElem[0]=null;
}
}
pElem[pElem.length] = new Option(pOptionText, pOptionVal, false,
false);
}
}
return checkFlag;
}
</script>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html;
charset=windows-1254">
</head>

<body>
<form name="form1" method="post" action="">
<select multiple size="4" name="select">
<option value="1Nilay" >Nilay1</option>
<option value="2Nilay">Nilay2</option>
<option value="3Nilay">Nilay3</option>
<option value="4Nilay">Nilay4</option>
<option value="5Nilay">Nilay5</option>
</select>
<input type="button" name="Submit" value="Selected list items"
onClick="addOption(document.forms[0].name+'.select2',
document.forms[0].name+'.select');">
<select size="4" name="select2">
</select>
</form>
</body>
</html>
Jul 23 '05 #1
1 1626
On 27 Aug 2004 06:34:34 -0700, NiLaY <ab*******@hotmail.com> wrote:
I have a function like the one below to move a selected option from one
listbox to another. But I want to be able to select multiple and be able
to move those selections to the other listbox. Can anyone help me on
that.
I've provided an example at the following address:

<URL:http://www.mlwinter.pwp.blueyonder.co.uk/clj/select/>

I've tested it on Opera, IE, Mozilla and NN4 and it works without any
problems. Unfortunately, the mark-up isn't valid as a SELECT element must
always have at least one OPTION element.

If I may, I'd like to make a few comments about your posted code.
<html>
<head>
<script language="JavaScript">
The language attribute has been deprecated in favour of the (required)
type attribute. The language attribute should not be used any more.

<script type="text/javascript">
function addOption(sBox, aBox) {
[snip]
pElem = eval("document."+sBox);
pOptionVal = eval("document."+aBox+".value");
You've done some very strange things with your function parameters which
makes those eval() call necessary. Really, they shouldn't be; eval() is
almost always the wrong thing to do, with faster, less error-prone
solutions available.

What you should have done was passed three arguments: the form, the first
box, and the second box. Assuming you changed the function signature to:

addOption(frm, src, dest)

You could write:

var oForm = document.forms[frm], // reference to the form
oSrc = oForm.elements[src],
oDest = oForm.elements[dest];

pOptionVal = oSrc.value;
//alert("selected items:\n" + pOptionVal);
if (pOptionVal != ""){
pOptionText =
eval("document."+aBox+"[document."+aBox+".selectedIndex].text");
Based on the changes above, you could replace that with:

pOptionText = oSrc[oSrc.selectedIndex].text;

[snip]
<meta http-equiv="Content-Type" content="text/html;
charset=windows-1254">
I think it would be a good idea to steer clear of Windows character sets.
Not everyone uses Windows! For most purposes, ISO-8859-1 (Latin) or ASCII
(though I forget it's character set name) would be better.

[snip]
<input type="button" name="Submit" value="Selected list items"
onClick="addOption(document.forms[0].name+'.select2',
document.forms[0].name+'.select');">


To accomodate the changes I've put forward, this would become:

<input ... onclick="addOption('form1','select','select2');">

[snip]

Hope that helps,
Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: palmiere | last post by:
Im trying to do the following: I have 2 select boxes, both are pre populated with data. The first box is a single select box, the 2nd is a multiple select box. Depending on the 1st selection, I...
7
by: Felix Natter | last post by:
hi, I have a php-Script which gets passed a Boat-ID and this is used to mark an element in a <select> as the default: <select name="boote" multiple="multiple" size="5"> <option...
3
by: Disco-181 | last post by:
Hi, I have a script which isn't working in Mozilla based browser for some reason. I'm trying to run two functions from the body onload tag and it simply isn't happening. I have a cascading...
2
by: areef.islam | last post by:
Hi, I am kinda new to javascript and I am having this problem with selecting multiple options from a select tag. Hope someone can help me out here. here is my code...
2
by: ggk517 | last post by:
I have a script like below: ------------------------------------------------------------------------------------------------------------------------ <html> <head> <title>TEST</title> <script...
4
by: bansh | last post by:
Hi all, As question title I need a javascript which provides the facility to make multiple selection without holding control key. Means I how is it possible to make multiple selection/deselction...
1
by: shidram | last post by:
Hello sir, I have problem while using javascript with HTML multiple selection tag, I want to write javascript in such a way that if a I change the option by clicking on the radio button on html...
92
by: bonneylake | last post by:
Hey Everyone, Well i was hoping someone could explain the best way i could go about this. i have a few ideas on how i could go about this but i am just not sure if it would work. Right now i...
58
by: bonneylake | last post by:
Hey Everyone, Well recently i been inserting multiple fields for a section in my form called "serial". Well now i am trying to insert multiple fields for the not only the serial section but also...
482
by: bonneylake | last post by:
Hey Everyone, Well i am not sure if this is more of a coldfusion problem or a javscript problem. So if i asked my question in the wrong section let me know an all move it to the correct place. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.