Skip to content

Instantly share code, notes, and snippets.

@crynobone
Created June 1, 2011 09:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crynobone/1002036 to your computer and use it in GitHub Desktop.
Save crynobone/1002036 to your computer and use it in GitHub Desktop.
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