Hi there,
You can use the :selected selector in combination with .length, to find out how many options have been selected:
HTML Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Select example</title>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<style>
#result{
width:300px;
margin:15px 0;
padding:5px;
border: 1px solid gray;
}
</style>
</head>
<body>
<form>
<select multiple="multiple" name="cars" id="cars">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
</form>
<p>Hold down the Ctrl (windows) / Command (Mac) button to select multiple options.</p>
<div id="result">Watch this space</div>
<script>
$(document).ready(function() {
$("#cars").click(function(){
if ($("#cars :selected").length == 1){
//call function one here
$("#result").html("Exactly one option selected")
$("#result").css("background", "yellow");
} else {
// or call function 2 here
$("#result").html("More than one option selected")
$("#result").css("background", "green");
}
})
});
</script>
</body>
</html>
HTH
Edit: Obviously I'm using jQuery, but I imagine that you could quite easily do this without
Bookmarks