Somehow the answers above did not work for me. Seems that you have to use properties instead of attributes. Here is the code:
$('#selectall').click(function() {
$('select#studentremain option').prop("selected",true);
});
$('#deselectall').click(function() {
$('select#studentremain option').prop("selected",false);
});
Documentation says that selected
attribute only specifies that an option
should be pre-selected when the page loads. But is not set when the actual option
is selected by the user.
Source