javascript - Changing SELECT ID With Loop -
i have form can add new entries on click using javascript. however, change id of each subsequent "add-on": e.g. first tbody has id of "participant1", next 1 have id of "participant2", eighth "participant8", , forth.
here excerpt main file:
<fieldset class="row2"> <legend>list of participants</legend> <p> <input type="button" value="add participant" onclick="addrow('datatable')" /> <input type="button" value="clear participants" onclick="deleterow('datatable')" /> <p>(all acions apply entries check marked check boxes only.)</p> </p> <table id="datatable" class="form" border="1"> <tbody> <tr> <p> <td> <input type="checkbox" required="required" name="chk[]" checked="checked" /> </td> <td> <div>participant: </div> <select name="participant1" id="participant1"> <option>person a</option> <option>person b</option> <option>person c</option> </select> </td> </p> </tr> </tbody> </table> <div class="clear"></div> </fieldset> and here attempting js file:
function addrow(tableid) { var table = document.getelementbyid(tableid); var rowcount = table.rows.length; if (rowcount < 50) { var row = table.insertrow(rowcount); var colcount = table.rows[0].cells.length; (var = 0; < colcount; i++) { var newcell = row.insertcell(i); //newcell.id="participant"+ rowcount; var cellbody = table.rows[0].cells[i].innerhtml; newcell.innerhtml = replaceall(cellbody, 'participant1', 'participant' + (rowcount + 1)); console.log(rowcount, newcell.innerhtml) } } else { alert("more 50 participants? sure?"); } } function replaceall(str, find, replace) { var = str.indexof(find); if (i > -1) { str = str.replace(find, replace); = + replace.length; var st2 = str.substring(i); if (st2.indexof(find) > -1) { str = str.substring(0, i) + replaceall(st2, find, replace); } } return str; } but appears taking in 1 participant?
you getting javascript errors. may need wrap functions in kind of dom-ready event.
the fiddle here works, using jquery , document ready check.
http://jsfiddle.net/brettwgreen/17om5xpm/
i'd recommend using (a) jquery , (b) knockout.js.
once start using knockout or similar js framework kind of thing, you'll never go back.
js:
$(document).ready(function() { window.addrow = function addrow(tableid) { var table = document.getelementbyid(tableid); var rowcount = table.rows.length; if(rowcount < 50){ var row = table.insertrow(rowcount); var colcount = table.rows[0].cells.length; console.log(colcount); for(var i=0; i<colcount; i++) { var newcell = row.insertcell(i); //newcell.id="participant"+ rowcount; var cellbody = table.rows[0].cells[i].innerhtml; newcell.innerhtml = replaceall( cellbody, 'participant1' , 'participant' + (rowcount+1) ); console.log(rowcount, newcell.innerhtml) } } else { alert("more 50 participants? sure?"); } } window.replaceall = function replaceall(str, find, replace) { var = str.indexof(find); if (i > -1) { str = str.replace(find, replace); = + replace.length; var st2 = str.substring(i); if(st2.indexof(find) > -1) { str = str.substring(0,i) + replaceall(st2, find, replace); } } return str; } });
Comments
Post a Comment