javascript - How can I make a loop split 4 inputs between two divs? -
i need make small behavior change block of code inherited, set unfortunately 1 large function building out html template , has large number of loops in it.
currently there section in template function loop generates 4 text inputs in containing <div>
along submit button. make if in case specific area has more 2 inputs put 2 inside of 1 <div>
subit , other 2 in div own submit, splitting them up. here quick , (very) dirty visual example of trying achieve.
and below current loop generating 4 inputs single <div>
. thought need set counter upon being === 2 create new html elements new classes style them need be. i'm unsure how achieve while still keeping loop going.
if (measurementtypegroup.measurementtypes.length > 1) { newnodehtml += "<div id='" + measurementclass + "-3-" + measurementtypegroup.measurementtypes[0].targetconfigure[0].targettype + "' class='target-metric input " + measurementclass + "-input'>"; (var k = 0; k < measurementtypegroup.measurementtypes.length; k++) { var mtype = measurementtypegroup.measurementtypes[k]; var usertarget = mtype.usertarget; var targetconfigure = mtype.targetconfigure[0]; newnodehtml += "<div><span>" + mtype.measurementtypename + " target</span>" + "<input type='text' id='txt-" + measurementclass + "-3-" + mtype.measurementtypeid + "' placeholder='" + mtype.unitname + "' value='" + targethighvalue + "' " + disableattribute + "></div>"; } newnodehtml += "<a onclick='cellclicked(" + mtypegroupid + ", 3, 3)'><span class='check-range'></span></a>" + "</div>"; }
thanks help!
this should need, nested loop
number of loops
, index
if (measurementtypegroup.measurementtypes.length > 1) { var index = 0; var numberofloops = math.ceil(measurementtypegroup.measurementtypes.length / 2); (var = 0; < numberofloops; i++) { var newnodehtml = ''; newnodehtml += "<div id='" + measurementclass + "-3-" + measurementtypegroup.measurementtypes[0].targetconfigure[0].targettype + "' class='target-metric input " + measurementclass + "-input'>"; (index; index < index + 2; index++) { if(measurementtypegroup.measurementtypes[index] === undefined) break; var mtype = measurementtypegroup.measurementtypes[index]; var usertarget = mtype.usertarget; var targetconfigure = mtype.targetconfigure[0]; newnodehtml += "<div><span>" + mtype.measurementtypename + " target</span>" + "<input type='text' id='txt-" + measurementclass + "-3-" + mtype.measurementtypeid + "' placeholder='" + mtype.unitname + "' value='" + targethighvalue + "' " + disableattribute + "></div>"; } newnodehtml += "<a onclick='cellclicked(" + mtypegroupid + ", 3, 3)'><span class='check-range'></span></a>" + "</div>"; } }
Comments
Post a Comment