I have an input like follows:
<input type="hidden" id="attachment-uuids" value="">
I'd like to be able to append a value to the Input, at different times:
$('#attachment-uuids).val('55555');
results in:
<input type="hidden" id="attachment-uuids" value="55555">
But then doing:
$('#attachment-uuids).val('66666');
results in:
<input type="hidden" id="attachment-uuids" value="66666">
Where I'd like the following:
<input type="hidden" id="attachment-uuids" value="55555, 66666">
how can I append values when the value is empty and when the value is not empty with a comma delimited list?
Thanks
$('#attachment-uuids').val(function(i,val) {
return val + (!val ? '' : ', ') + '66666';
});
EDIT: As @mkoryak noted, I'm doing an unnecessary negation of val
in the conditional operator. It could be rewritten without the !
as:
(val ? ', ' : '')
Just add a conditional when you add the value to the field.
var cur_val = $('#attachment-uuids').val();
if(cur_val)
$('#attachment-uuids').val(cur_val + "," + new_val);
else
$('#attachment-uuids').val(new_val);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With