2

I am trying to convert JSON to HTML. I am able to create the HTML element and ID attribute. However I can not create the onclick events. I am using the npm json2htmll.js npm module.

let jsontoHtml: {

                "<>": "div",
                "html": [
                     {
                        "<>": "button",
                        "id":"button1",
                        "onclick": "ButtonClicked()",
                        "text": "Click"
                    }
                ]
            };

let html = json2html.transform({}, jsontoHtml);

Current output:

</button id="button1">click</button>

Expected output:

<button id="button1" onclick="buttonClicked()">Click</button>

Do you know how I can achieve the expected output?

Greg
  • 4,468
  • 3
  • 16
  • 26

1 Answers1

1

json2html doesn't support on events using the npm module. Instead I would look at using the jquery plugin for json2html. You'll be able to add events there that will directly hook into the jquery event. See this example on json2html.com

{'<>':'button','onclick':function(e){
    //e.event : the jQuery event
    //e.data  : user specified data from the the json2html options variable eventData (see Usage)
    //e.obj   : the source object that we are transforming
    //e.index : the index of the array in which the source object was found
}}
Chad Brown
  • 1,627
  • 1
  • 13
  • 22