Created
June 1, 2011 09:32
-
-
Save crynobone/1002036 to your computer and use it in GitHub Desktop.
Retrieve and reassigning multiselect value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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