I have array stored inside input hidden field as follow:
<input type='text' name='fileuploaddelete' id='fileuploaddelete' class='required' value='["ppl.jpg","Tulips.jpg","Penguins.jpg"]'/>
JS
var deleteFile = [];
deleteFile = $("#fileuploaddelete").val(); // return string format
When I retrieved the value using jquery, it is in String format. How to keep it in array format? Any ideas?
asked Apr 4, 2015 at 7:58
user2991183
6831 gold badge12 silver badges23 bronze badges
3 Answers 3
use
parsedTest = JSON.parse( $("#fileuploaddelete").val());// this will convert it into array
answered Apr 4, 2015 at 8:02
Saty
22.5k7 gold badges35 silver badges52 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
var arr = JSON.parse(deleteFile);
answered Apr 4, 2015 at 8:03
Hamid Mohayeji
4,2953 gold badges52 silver badges60 bronze badges
Comments
Remove
"
and
[]
before setting the value. Then you can use:
$("#fileuploaddelete").val().split(',');
This should return the value in array format.
answered Apr 4, 2015 at 8:02
Legends
22.9k17 gold badges102 silver badges133 bronze badges
4 Comments
Regent
What about removing
", [ and ] before voting up?Regent
Someone upvoted your answer while it is not actually correct one: instead of
ppl.jpg, Tulips.jpg, Penguins.jpg you got ["ppl.jpg", "Tulips.jpg", "Penguins.jpg"]Legends
Yes, you are right. He has to remove these unnecessary characters before setting the value.
Regent
So we come to two thoughts: 1. Your answer is incomplete one. 2. Does it make sense to reinvent the wheel?
lang-js
JSON.parse()to retrieveObject(Arrayin this case) fromString. Fiddle.