PDA

View Full Version : No Duplicates Chosen in Drop-Down lists



josman
03-04-2015, 10:22 AM
hi all,

i've found the next great code in this javascript forum: http://www.dynamicdrive.com/forums/showthread.php?33533-No-Duplicates-Chosen-in-Drop-Down-lists


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
<!--

function uniqueCoicesSetup(){
var warning = 'Only one of each item May be Selected from each Group!',
s = document.getElementsByTagName('select'),
f = function (e){
var s = uniqueCoicesSetup.ar;
for (var o = this.options.selectedIndex, i = s.length - 1; i > -1; --i)
if(this != s[i] && o && o == s[i].options.selectedIndex){
this.options.selectedIndex = 0;
if(e && e.preventDefault)
e.preventDefault();
alert(warning);
return false;
}
},
add = function(el){
uniqueCoicesSetup.ar[uniqueCoicesSetup.ar.length] = el;
if ( typeof window.addEventListener != 'undefined' )
el.addEventListener( 'change', f, false );
else if ( typeof window.attachEvent != 'undefined' ){
var t = function() {
return f.apply(el);
};
el.attachEvent( 'onchange', t );
}
};
uniqueCoicesSetup.ar = [];
for (var i = s.length - 1; i > -1; --i)
if(/nodupe/.test(s[i].className))
add(s[i]);
}

if(typeof window.addEventListener!='undefined')
window.addEventListener('load', uniqueCoicesSetup, false);
else if(typeof window.attachEvent!='undefined')
window.attachEvent('onload', uniqueCoicesSetup);
// -->
</script>
</head>
<body>
<div>
<select class="nodupe" name="l1">
<option value="" selected>Choose</option>
<option value="">Bob</option>
<option value="">Carol</option>
<option value="">Ted</option>
<option value="">Alice</option>
<option value="">Tom</option>
<option value="">Harry</option>
</select>

<select class="nodupe" name="l2">
<option value="" selected>Choose</option>
<option value="">Bob</option>
<option value="">Carol</option>
<option value="">Ted</option>
<option value="">Alice</option>
<option value="">Tom</option>
<option value="">Harry</option>
</select>
</div>
</body>
</html>

i know is an older code, but i'm using it for a customer and it works fine.

i only would like to know the following

sahould it be possible to disable/hide the selected options choosen in a previous dropdown-list?

my purpose is that user don't see the selected options or at least to have it disabled

thanks in advance for your help