0

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
1
  • 3
    You can use JSON.parse() to retrieve Object (Array in this case) from String. Fiddle. Commented Apr 4, 2015 at 8:00

3 Answers 3

4

use

parsedTest = JSON.parse( $("#fileuploaddelete").val());// this will convert it into array
answered Apr 4, 2015 at 8:02
Sign up to request clarification or add additional context in comments.

Comments

1
var arr = JSON.parse(deleteFile);
answered Apr 4, 2015 at 8:03

Comments

1

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

4 Comments

What about removing ", [ and ] before voting up?
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"]
Yes, you are right. He has to remove these unnecessary characters before setting the value.
So we come to two thoughts: 1. Your answer is incomplete one. 2. Does it make sense to reinvent the wheel?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.