I was just wondering why doesn’t this script works? I’m trying to get the value of the selected (one or many items) options in a SELECT MULTIPLE. What is missing?
Thanks
<script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
<script>
$('#ooo').change(function() {
var foo = [];
$('#ooo :selected').each(function(i, selected){
foo += $(selected).valueOf();
});
alert(foo);
//$("#test").text(str);
});
</script>
</head>
<body>
<select name="test" id="ooo" multiple="multiple">
<option value="1">test</option>
<option value="2">bbbb</option>
<option value="3">aaaa</option>
<option value="4">xxx</option>
<option value="5">zzz</option>
</select>
You need to wrap the code in the
$(document).ready(). the jQuery ready() handler will wait unitl the document elements are loaded, before binding the change handler to the element. Also wen using select multiple it will return an array of selected items. Because of this you will want to run .each() on the val() of the select element.