Solved

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

Posted on 2010-09-20
10
456 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
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 40

Accepted Solution

by:
gurvinder372 earned 500 total points
Comment Utility
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:ddsh79
Comment Utility
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
Comment Utility
I try your code, it display all the values even the selected one.
0
 

Author Comment

by:sitijaafar
Comment Utility
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:ddsh79
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 40

Expert Comment

by:gurvinder372
Comment Utility
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
Comment Utility
yes, it work, I try to apply to my code.
0
 

Author Comment

by:sitijaafar
Comment Utility
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
Comment Utility
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
Comment Utility
@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

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!

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

771 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

14 Experts available now in Live!

Get 1:1 Help Now