How to pass jsonarray through dynamic created button event and also access these jsonarray element?


var jsonArr =[
             { " name"    `:"John", "age":31, "city":"New York" },
             { "name":"Smith", "age":25, "city":"Dubai" }

function create()
function createButton(jsonArr)
alert('inside function jsonarray length: '+jsonArr.length);
var htmlcode="<input type=\"button\" id=\"subbtn\" value=\"Dynamic SUBMIT Button\" onclick=\"submitdata(,'"+jsonArr+"');\" ";

document.getElementById("dynamic").innerHTML = htmlcode;

function submitdata(id,json_arr)
alert('Button id is: '+id);
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 =;
      //              var age=_el.age;
        //            var;
                  //  alert('Name: '+name);

Then create a normal button and value=“Click to Generate Button” onclick=“create()”
<input type="button" id="testid" value="Click to Generate Button" onclick="create()"/>

Actual json array length is 2;

but when I pass this jsonarray through submitdata() event then I calculate jsonarray length is 31 and also can’t find out jsonarray element.(show undefined).

Please solve my problem. I wand to access jsonarray element inside submitdata().

I don’t know if it’s in your code or was introduced here, but there’s a stray backtick that could cause an undefined

{ " name"    `:"John",

var jsonArr =[

         { " name" :"John", "age":31, "city":"New York" },
         { "name":"Smith", "age":25, "city":"Dubai" }

by typing mistake… but this is correct…

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.