Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Retrieve and reassigning multiselect value
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
</head>
<body>
<select id="the-list" multiple="multiple">
<option id="list-1" value="1">Hello World</option>
<option id="list-2" value="2">Foo</option>
<option id="list-3" value="3">Foobar</option>
</select>
<input type="text" id="list" readonly="readonly" value="" />
<button id="update">Update</button>
<button id="reset">Reset</button>
<script type="text/javascript">
jQuery(function($) {
$('#update').bind('click', function() {
var values = $('#the-list').val();
$('#list').val(values.join(','));
$('#the-list').val('');
});
$('#reset').bind('click', function() {
var values = $('#list').val();
var value = values.split(',');
// you can actually set $('#id').val(['1', '2']);
$('#the-list').val(value);
});
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment