Solved

How to get all the not selected value of multiple list box

Posted on 2010-09-20
10
460 Views
Last Modified: 2012-05-10
Hi experts,
How to get all the not selected values of multiple list box.  For selected : elSel.options[k].selected

So, what the javascript for not selected values.
0
Comment
Question by:sitijaafar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 40

Accepted Solution

by:
gurvinder372 earned 500 total points
ID: 33722851
using javsacript, you can get all the values in select box which are unselected in a hidden field.
Then you can send those values (comma separated) to servlet after submitting them

try this code
<html>
<head>
<script type="text/javascript">
function unselectedOptions()
{
  var options = document.getElementById("mySelect").options;   
  var unSelectedVals = new Array();
  for ( var counter =0; counter < options.length; counter++ )
  {
     if ( !options[ counter ].checked )
     {
         unSelectedVals[ unSelectedVals.length ] = options[ counter ].value;
     }
  }
  alert(unSelectedVals.join(","));
}
</script>
</head>
<body>

<form>
<select id="mySelect" size="4" multiple>
	<option>Apple</option>
	<option>Pear</option>
	<option>Banana</option>
	<option>Orange</option>
</select>
<a href="javascript:unselectedOptions()">Show all Unselected options</a>
</form>
</body>
</html>

Open in new window

0
 
LVL 14

Expert Comment

by:Dushyant Sharma
ID: 33722915
elSel.options[k].selected itself returns boolean.
so checking it for false will return all the not selected values.

you can check as
if (elSel.options[k].selected==false)
0
 

Author Comment

by:sitijaafar
ID: 33722924
I try your code, it display all the values even the selected one.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:sitijaafar
ID: 33722931
so, here my code.
function removeCountrySelected()
{
  var elSel = document.getElementById("learnCountry1");
  var i; 
  var alrmsg;
  var country;
  var countries;
  i
  for (i = elSel.length - 1; i>=0; i--) {
    if (elSel.options[i].selected) {	  
      elSel.remove(i);
    }	
	
	 if (elSel.options[i].selected==false) {	  
        country=elSel.options[i].value;
		countries+=country;
    }	
	document.frm.learnCountry.value=countries;

  
}

Open in new window

0
 
LVL 14

Expert Comment

by:Dushyant Sharma
ID: 33722937
try this
<script type="text/javascript">

var myselect=document.getElementById("sample")
for (var i=0; i<myselect.options.length; i++){
 if (myselect.options[i].selected==false){

  alert(i + "is not selected");
 }
}
</script>
0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33722938
Sorry, i need to use the 'selected' method, not the 'checked'

replace that method with
function unselectedOptions()
{
  var options = document.getElementById("mySelect").options;  
  var unSelectedVals = new Array();
  for ( var counter =0; counter < options.length; counter++ )
  {
     if ( !options[ counter ].selected )
     {
         unSelectedVals[ unSelectedVals.length ] = options[ counter ].value;
     }
  }
  alert(unSelectedVals.join(","));
}
0
 

Author Comment

by:sitijaafar
ID: 33723006
yes, it work, I try to apply to my code.
0
 

Author Comment

by:sitijaafar
ID: 33723028
I apply the code but not succeed.  
function removeCountrySelected()
{
  var elSel = document.getElementById("learnCountry1");
  var i; 
  var alrmsg;
  var country;
  var countries;
  var unSelectedVals = new Array();
  for (i = elSel.length - 1; i>=0; i--) {
    if (elSel.options[i].selected) {	  
      elSel.remove(i);
    }	
	
	if ( !elSel.options[i].selected )
     {
         unSelectedVals[ unSelectedVals.length ] = elSel.options[i].value;
     }	
	
  
}
document.frm.learnCountry.value=unSelectedVals.join(",");


}

Open in new window

0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 33723203
This should work

function removeCountrySelected() {
  var elSel = document.getElementById("learnCountry1");
  var unSelectedVals = [];
  for (var i = elSel.length - 1; i>=0; i--) {
    if (elSel.options[i].selected) elSel.options[i].remove();
    else unSelectedVals[unSelectedVals.length] = elSel.options[i].value;
  }
  document.frm.learnCountry.value=(unSelectedVals.length >0)unSelectedVals.join(","):"";
}

Open in new window

0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33723244
@sitijaafar: line 14-17 was not required, since once you have removed it from select box, it wont be accessible again,

Also remove line 21 now, since learnCountry select box now has only unselected value

0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Adding a countdown to HTA 12 95
JS library vs Js framework 11 20
selected value 16 24
Html date format 9 20
Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

735 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