javascript - bind a onclick function with object as parameter on anchor tag created in a loop -
i getting json data.and looping through , each data creating anchor tag , onclick of trying call function object parameter.
something
$.getjson('/ui_dashboard/rest/getalertdefvsalertvalues',function(data) { console.log(data) $("#checkallalertstbody").empty(); var mailservertbody=""; var objdata = data.getalertdefvsalertvalues; var objlen = objdata.length; for(var i=0;i<objlen;i++){ //one object key in json var alertwholeobject = { alertfunctionid : objdata[i].alertfunctionid, alertmessage : objdata[i].alertmessage, definitionname:objdata[i].definitionname, emailmessage : objdata[i].emailmessage, emailsubject:objdata[i].emailsubject, emailtemplate : objdata[i].emailtemplate, hubid:objdata[i].hubid, id : objdata[i].id, isdefinitionenabled:objdata[i].isdefinitionenabled, mandatoryparam : objdata[i].mandatoryparam, scaninterval:objdata[i].scaninterval, smsmessage : objdata[i].smsmessage, smstemplateid:objdata[i].smstemplateid, tenantid : objdata[i].tenantid, timestamp:objdata[i].timestamp, list: objdata[i].list, isarray : function(what){ return object.prototype.tostring.call(what) === '[object array]'; } }; console.log(alertwholeobject.alertmessage) var idwithoutspace = alertwholeobject.id.replace(/ /g, ''); //for overview table mailservertbody = '<tr><td>'+(i+1)+'</td><td>'+alertwholeobject.definitionname+'</td><td>'+alertwholeobject.timestamp+'</td><td>'+ '<div class="controls center">'+ '<a class="btn btn-info btn-mini" id="edit_'+idwithoutspace+'" onclick="editalertrules(' + alertwholeobject + ');"><i class="icon-edit icon-white"> </i></a>'+ '<a class="btn btn-danger btn-mini" id="delete_'+idwithoutspace+'" ><i class="icon-trash icon-white"> </i></a>'+ '</div></td></tr>'; $("#checkallalertstbody").append(mailservertbody); } }); }
but onclick function not working.in console showing [object object]
how send object created in loop click of each anchor tag.
please help
edited:
sorry, jumped gun on this. here update:
do not pass object parameter. instead pass index. declare object array outer scope (or global scope).
var obj = [];
inside for
loop instantiate object in array @ current index:
for (i=0; ....) { obj[i] = new object; ... }
now, when specifying inline onclick
, pass index:
mailservertbody = "... <a ... onclick='editalertrules(" + + ");'" ...</a>.."
inside function editalertrules
use object in outer scope index argument:
function editalertrules(index) { console.log(obj[index]); ... }
check fiddle idea: http://jsfiddle.net/n8hsj/1/
hope helps.
Comments
Post a Comment