function listbox_selectall(listID, isSelect) {
var listbox = document.getElementById(listID);
for(var count=0; count < listbox.options.length; count++) {
listbox.options[count].selected = isSelect;
}
}
Code language: JavaScript (javascript)
The above javascript code is very straight forward. All you have to do it to pass the ID of listbox you need to perform select all/deselect all and a boolean value for select/deselect. For example if a listbox has an id “countryList” following can be the function call. listbox_selectall('countryList', true); //select all the options
listbox_selectall('countryList', false); //deselect all the options
Code language: JavaScript (javascript)
function listbox_move(listID, direction) {
var listbox = document.getElementById(listID);
var selIndex = listbox.selectedIndex;
if(-1 == selIndex) {
alert("Please select an option to move.");
return;
}
var increment = -1;
if(direction == 'up')
increment = -1;
else
increment = 1;
if((selIndex + increment) < 0 ||
(selIndex + increment) > (listbox.options.length-1)) {
return;
}
var selValue = listbox.options[selIndex].value;
var selText = listbox.options[selIndex].text;
listbox.options[selIndex].value = listbox.options[selIndex + increment].value
listbox.options[selIndex].text = listbox.options[selIndex + increment].text
listbox.options[selIndex + increment].value = selValue;
listbox.options[selIndex + increment].text = selText;
listbox.selectedIndex = selIndex + increment;
}
Code language: JavaScript (javascript)
Thus, you can call this function with first argument as list id and second argument a string value ‘up’ or ‘down’ depending on where you want to move the selected option. listbox_move('countryList', 'up'); //move up the selected option
listbox_move('countryList', 'down'); //move down the selected option
Code language: JavaScript (javascript)
function listbox_moveacross(sourceID, destID) {
var src = document.getElementById(sourceID);
var dest = document.getElementById(destID);
for(var count=0; count < src.options.length; count++) {
if(src.options[count].selected == true) {
var option = src.options[count];
var newOption = document.createElement("option");
newOption.value = option.value;
newOption.text = option.text;
newOption.selected = true;
try {
dest.add(newOption, null); //Standard
src.remove(count, null);
}catch(error) {
dest.add(newOption); // IE only
src.remove(count);
}
count--;
}
}
}
Code language: JavaScript (javascript)
Just call the function with two arguments, first is the source listbox id and second is the destination listbox id. listbox_moveacross('countryList', 'selectedCountryList');
Code language: JavaScript (javascript)
<script>
function submit() {
listbox_selectall('righthand_side_listbox_id', true);
return true;
}
<form onsubmit="return submit()">
...
</form>
Code language: JavaScript (javascript)
This would select all the options before form gets submitted. Now you can retrieve options in your server side script from request as request parameter. Java URL Encoder/Decoder Example - In this tutorial we will see how to URL encode/decode…
Show Multiple Examples in OpenAPI - OpenAPI (aka Swagger) Specifications has become a defecto standard…
Local WordPress using Docker - Running a local WordPress development environment is crucial for testing…
1. JWT Token Overview JSON Web Token (JWT) is an open standard defines a compact…
GraphQL Subscription provides a great way of building real-time API. In this tutorial we will…
1. Overview Spring Boot Webflux DynamoDB Integration tests - In this tutorial we will see…
View Comments
Thanks a lot.
How can i get right side list when i submit
Nice article, I am searching for the same using jQuery....
And coming to the question which Agniraj has mentioned, Agniraj here is your answer, just check out the length of right list box and loop through the options, you will get all the options which are there in right list box.
Hope this might :)
Thanks,
Vivek
when i am trying it is giving me error as The function add(___newOption0, null) is undefined for the type Element
Gracias... no sabes del lio en que me sacaste.... te lo agradezco mucho.
Thanks for the script, it helped me in right time to finish the project.
Please email me on how could i select all the values on the listbox if my listbox name is like this
[code] [/code]
this is my javascript for selecting all the listbox items:
[code]
function selectall()
{
for(i=0;i<document.forms['frmcustom'].sections[].length;i++){
document.forms['frmcustom'].sections[i].selected=true;
}
} [/code]
Nice article..
May be this wil help full but i need add,remove, add all, and remove all otion i the list
Thank you very much just what i needed
I noticed a few things while implementing this in PHP I'd like to share:
1) If you provide a name property to the destination list box, the "name=" property has to be appended with '[]' at the end, to return an array of items, otherwise it only returns the first item in the destination list box. For example
Use: ...
Not: ...
2) In the script function "listbox_moveacross(sourceID, destID)", the value of src.options.length needs to reflect the current sizeof src.options.length thru each iteration. In my implementation, I captured src.options.length into a local var ahead of the loop, in hopes of gaining some tick ecomony.
So I did:
var maxx = src.options.length;
for(var count=0; count < maxx; count++)
{
...
}
versus the example:
for(var count=0; count < src.options.length; count++)
{
...
}
I found out the hard way, I had to decrement var maxx along with the var count-- decrement. So the code line after count--; became maxx--;
if(src.options[count].selected == true)
{
...
count--;
maxx--;
}
Just a lesson learned.
Hi Darla,
I have only just started programing and can not get the code you changed working for the select box more right. Can you post or e-mail me the javascript?
How do I get the variables in PHP??
$rightselect = mysql_real_escape_string($_POST['rightselect']);
? My seclect id and name are rightselect
Many thanks for your help!