1

I am using dropzone.js to send some files to my server and the response that comes back is always the id of the attachment which is great. Happy that's finally working.

However, I am trying to populate an input field with a comma separated string of IDs as they are returned from the server. This may be when images are first uploaded, or when further images are added.

I need to take the value of an input, then add to it. I can't figure out where Im going wrong.

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- The HTML Input when no images are uploaded -->
<input type="hidden" id="media-ids" name="media-ids" value="">

<!-- The HTML Input (expected when images are uploaded) -->
<input type="hidden" id="media-ids" name="media-ids" value="12,13,14,15">
// inside the jQuery (ajax success function)

var current = jQuery('#media-ids').val();
var currentArray = current.split(',');
var newArray = currentArray.push(response);
var inputString = newArray.join(); //ERROR - .join() is not a function

jQuery('#media-ids').val(inputString);

console.log(inputString); //Returns 2 (I'm guessing that's false)
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Alex Knopp
  • 907
  • 1
  • 10
  • 30
  • 2
    Nothing to do with jQuery. You have the vanilla JS line `var newArray = currentArray.push(response);` which will push a value to `currentArray` but assign *the new length of `currentArray`* to the variable `newArray`. You then try to use `newArray` as if it's an array, yet it's guaranteed to be a number. – VLAZ Jan 14 '20 at 13:35
  • 2
    `.push()` doesn't return a new array, but mutates the existing one. That's where the error is. – Samuli Hakoniemi Jan 14 '20 at 13:35

0 Answers0