<script>
var maxSel = 3;
var selected =[];
window.onload=function() {
saveSel(document.getElementById('sel')); // initialise
}
function saveSel(sel) {
for (var i=0;i<sel.options.length;i++) {
selected[i]=(sel.options[i].selected);
}
}
function resetSel(sel) {
for (var i=0;i<sel.options.length;i++) {
sel.options[i].selected = selected[i];
}
}
function test(sel) {
for (var i=0,newCnt=0;i<selected.length;i++) {
newCnt+= (sel.options[i].selected)?1:0;
}
if (newCnt>maxSel) {
alert('Sorry, you can only select a maximum of '+maxSel);
resetSel(sel);
}
else saveSel(sel); // store the new
}
</script>
<select id='sel' multiple onChange="test(this)">
<option value='1'>One</option>
<option value='2'>Two</option>
<option value='3'>Three</option>
<option value='4'>Four</option>
<option value='5'>Five</option>
</select>
|