I am trying to assign the responseText of an XHR request to an object variable that I can then pass thru json2html in order to create a form. The problem I am having is the json I'm passing has a function assigned to the onclick event of an item on the form. This keeps me from being able to convert the responseText string using JSON.parse() as it errors out on the function call. I know that the json will work if I'm able to call it as an object but it wont let me create the object; I know that I can use the ready() function but would prefer to pass the function thru the json2html call. I imagine it's something simple I'm overlooking but need some help figuring out how I can get the string into a usable format. As an example:
//works fine
var x= {
"<>":"div",
"class":"",
"text":"test",
"onclick": function(
{
console.log("click")
}
};
$('#content').json2html({},x);
//does not work
x=$.ajax({url:u});
x.complete(function(){
//responseText:
{
"<>":"div",
"class":"btn p-3 bg-white test",
"text":"test",
"onclick": function(){console.log("click")}
}
//.responseText
$('#content').json2html({},x["responseText"]);
});
//.does not work
Firstly your responseText is just that text, make sure you jsonParse the text first into a json object before you send it to json2html.
Secondly you won't be able to transfer the onclick function via ajax, that's something that will need to be added client side.