shmshd17 shmshd17 - 6 months ago 28
Javascript Question

JSON - How to give onclick element in script?

How to give onclick function for id in JavaScript? eg: my onclick function is

. My each buttons contain their own
's. But I couldnt figure out how to use this element to generate using JSON.

I prefer JavaScript rathar than jquery.

function myFunction(arr) {
var out = "";
var i;
for(i = 0; i<arr.length; i++) {
out += '<button onclick="function('#' + arr[i].ids + '')">' +
arr[i].blaah + '</button><br>';
document.getElementById("id01").innerHTML = out;


Simple solution: use this

function yourfunction(elem){
//elem contains the button
//you could do:"none";

In your html do:

<button onclick="yourfunction(this)">Button</button>

For shure this can be created dynamically. If you want to keep your code, dont get confused with " and ' . " ' " for example: the second " closes the first, even if theres an opened '. And you cannot name a function function. Thats an syntax error.