html - dynamically creating a table and adding cells to the table through a function with Javascript -
i wondering how create function loop creates new cells / rows other loop call upon. function should return newrow should specified amount of cells. idea html code displays 3 images per row, if there 2 images needs 2 cells. first if / else statement.
here code far..
var cells = document.getelementsbytagname('td'); var cell = '<td>' + cells[0].innerhtml + '</td>'; //console.log(cell); document.getelementbyid('searchbtn').onclick = search; //specified 3 per row var numperrow = 3; //gets number form text input var num = document.getelementbyid("searchtxt").value; function search(){ //var num = 4; console.log(num); //loop once per row var htmlstr = ''; (var = 0; < num; = + numperrow){ //htmlstr += '<tr>' + cell + cell + cell + '</tr>; if (num - >= numperrow) { //displays new row of 3 htmlstr += newrow(numperrow); }else { //less 3 display htmlstr += newrow(num - i); } } document.getelementbyid('thumbnails').innerhtml = htmlstr; } /* *returns html new row. * numtoadd: number of cells add row. * */ //this function not know how write function newrow(cellstoadd){ /////?????????? should loop return new row for loop above }
}
here simple function if don't want pass values can leave content
out.
function newrow(numberofcells, content) { var result = '<tr>'; for(var = 0; < numberofcells; i++) result += '<td>' + content[i] + '</td>'; result += '</tr>'; return result; }
Comments
Post a Comment