I am trying to create a function which updates the hidden inputs dynamically.
Below is my function:
function updateDeletedFileIds(deletedFileId) {
if($('input[name=multiFieldDeletedIds]') == null) {
$('.selected_file_info').append('<input type="hidden" class="MultiField" name="multiFieldDeletedIds" value="'+deletedFileId+'" />');
}
else{
id = $('input[name=multiFieldDeletedIds]').val();
id += ','+deletedFileId
$('input[name=multiFieldDeletedIds]').attr('value', id);
}
}
Here is my expected output: when the updateDeletedFileIds function is called for the first time it should create a hidden input tag and set the value as per deletedFileId
updateDeletedFileIds(1) =>
'<input type="hidden" class="MultiField" name="multiFieldDeletedIds" value="1" />'
when it is calleda second time it should just update the value as comma separated
updateDeletedFileIds(2) =>
'<input type="hidden" class="MultiField" name="multiFieldDeletedIds" value="1,2" />'
updateDeletedFileIds(3) =>
'<input type="hidden" class="MultiField" name="multiFieldDeletedIds" value="1,2,3" />'
But my function is not working as expected. Could someone please help me out?