javascript - JSON - How to give onclick element in script? -
how give onclick function id in javascript? eg: onclick function oclick="function('#id')"
, need action made in div. each buttons contain own id
's. couldnt figure out how use element generate using json.
i prefer javascript rathar 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; }
do need this?
var arr = ["id1", "id2", "id3", "id4"] function onclick() { alert("you clicked button id: " + this.id); } function createbuttons(arr) { var out = document.getelementbyid("out"); (var = 0, l = arr.length; < l; i++) { var node = document.createelement("button"); node.id = arr[i]; node.innerhtml = "button " + arr[i]; node.addeventlistener("click", onclick, false); out.appendchild(node); } } createbuttons(arr);
<div id="out"> </div>
Comments
Post a Comment