Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing JSON Array in a Hidden HTML element

I have a JSON Array that is defined as follows:

var myItems = {
  "data": [
    { "id":1, "firstName":"bill", "lastName":"smith" },
    { "id":2, "firstName":"john", "lastName":"apple" },
    { "id":3, "firstName":"will", "lastName":"long"}
  ]
};

I need to store this array in a hidden HTML element so that I can pass it to my server-side code in string format. My problem, I'm not sure how to do this. Can someone tell me how to do this?

Thank you!

like image 375
Villager Avatar asked Jan 20 '23 23:01

Villager


1 Answers

Essentially, you want to serialize your array into json, and then specify where you want to store the resulting string value..

document.getElementById('yourHiddenField').value = jsonString;
like image 122
Dutchie432 Avatar answered Jan 30 '23 19:01

Dutchie432