In the following SCRIPT code snippet:
var jsonArr = [
{
" name": "John",
"age": 31,
"city": "New York"
},
{
"name": "Smith",
"age": 25,
"city": "Dubai"
}
];
function create()
{
createButton(jsonArr);
}
function createButton(jsonArr)
{
alert('inside function jsonarray length: ' + jsonArr.length);
alert(jsonArr);
var htmlcode = "input type=\"button\" id=\"subbtn\" value=\"Dynamic SUBMIT Button\" onclick=\"submitdata(this.id,'" + jsonArr + "');\"";
document.getElementById("dynamic").innerHTML = htmlcode;
}
function submitdata(id, json_arr)
{
alert('Button id is: ' + id);
alert(json_arr);
alert('inside submit data jsonarray length: ' + json_arr.length);
// for (var i = 0; i < json_arr.length; i++)
//{
// var _el = json_arr[i];
// var name = _el.name;
// var age=_el.age;
// var city=_el.city;
// alert('Name: '+name);
//}
}
Next, include a standard button with a value of "Click to Generate Button" and an onclick event of "create()".
The actual length of the JSON array is 2;
However, when passing this JSON array through the submitdata() event, the calculated length of the JSON array is 31 and the elements in the JSON array are undefined.
Could someone assist me in resolving this issue? I need to be able to access the elements of the JSON array inside submitdata().