Problem with onclick
maxsmaleha opened this issue · comments
Hello.
I try to set a click event but I have nothing. I do everything as written in the documentation. Could you tell me what I'm doing wrong?
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.json2html/1.2.0/jquery.json2html.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/json2html/1.2.0/json2html.min.js"></script>
<script>
var t = {
"<>": "button",
"onclick": function(e) { console.log(123);},
"html": "text"
};
//t={'<>':'button','html':'click me','onclick':function(e){console.log(444);} };
document.write( json2html.transform({},t) );
</script>
A couple things you are going to want to change here.
a) the order of the scripts, json2html main library should load first before the jquery plugin.
b) you need to use the jquery plugin function to attach the html rather than the main json2html.transform if you want to use events. Events can only be used with the jquery plugin, see more information here http://json2html.com/docs/#main-attributes-events
see code changes below
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/json2html/1.2.0/json2html.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.json2html/1.2.0/jquery.json2html.min.js"></script>
<script>
var t = {
"<>": "button",
"onclick": function(e) { console.log(123);},
"html": "text"
};
$('body').json2html({},t);
</script>