<input type="hidden" id="values" value="1,2,1,3" />
<a href="#" id="add" data-value="4">add</a>
<a href="#" id="remove" data-value="1">remove</a>
<script type="text/javascript">
$(document).ready(function()
{
$('#add').click(function()
{
var value = $(this).attr('data-value');
//add to $('#values')
return false;
});
$('#remove').click(function()
{
var value = $(this).attr('data-value');
//remove all values that match in $('#values');
return false;
});
});
</script>
Examples
a) Add, output would be: 1,2,1,3,4
b) Remove, output would be 2,3
You can achieve this with basic JavaScript functions and some jQuery goodies. Have a look at the documentation of the individual functions to learn more about them.
Add:
Remove:
Reference:
val,split,push,join,grepDEMO