dynamic html buttons in javascript code -
so using javascript, jquery, , html here. have dynamic number of buttons being created, , each call function using unique variables. variables held in json variable. here code is:
var box = "<font size=\"2\">the following assassins in current location:<br/><table width = \"100%\">"; (var i=0; i<info.length; i++) { if(userid != info[i].playerid){ box += "<tr><td>"+info[i].name+" | rank: "+info[i].rank+"</td><td align=\"right\"><input id='attack' type='button' onclick='loadattack(userid, info[i].playerid, info[i].name, info[i].rank, location)' value='attack'/></td></tr>"; } } box += "</table></font>"; $("#assassinbox").html(box);
the box looks fine, proper names, ranks, , buttons. problem when button pushed, info
undefined. think because button doesn't own copy of it, , out of bounds of array @ end of loop. struggling think of solution, way of passing onclick function unique variable?
thanks!
box += "<tr><td>"+info[i].name+" | rank: "+info[i].rank+"</td><td align=\"right\"><input id='attack' type='button' onclick='loadattack(userid, info["+i+"].playerid, info["+i+"].name, info["+i+"].rank, location)' value='attack'/></td></tr>";
that should work. although if consider rewriting not use inline event handlers , maybe building html jquery or native domelement creation methods rather concatenating strings of html. makes lot more maintainable in long run.
Comments
Post a Comment