With this script you can add rows to an existing table. But it puts the row not after but in front of existing rows.
How to get this script to put rows after the last?
HTML :
<button onclick="myFunction()">Voeg meer tickets toe</button>
JS :
function myFunction() {
var table = document.getElementById("myTable");
var row = table.insertRow(0);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
var cell4 = row.insertCell(3);
var cell5 = row.insertCell(4);
cell1.innerHTML = '<input type="text" name="naamoptie2[0]" class="simple_field" placeholder="Naam van optie/onderdeel" required style="padding: 10px 10px;" />';
cell2.innerHTML = '€ <input type="number" name="prijsticket[0]" class="simple_field" placeholder="100" required style="padding-top: 5px; padding-bottom: 5px; min-height: 30px;" step="any" />';
cell3.innerHTML = '<input type="number" name="aantaltickets2[0]" class="simple_field" required style="padding-top: 5px; padding-bottom: 5px; min-height: 30px;" placeholder="aantaltickets" />';
cell4.innerHTML = '<input type="text" name="onderdeelnummer[0]" class="simple_field" required style="padding: 10px 10px; margin-top: 10px;" placeholder="Onderdeelnummer" />';
cell5.innerHTML = '<input type="text" name="categorienummer[0]" class="simple_field" required style="padding: 10px 10px; margin-top: 10px;" placeholder="Categorienummer" />';
}
Change
var row = table.insertRow(0);
to
var row = table.insertRow( table.rows.length );
so that each row is added to the end.
Since you have tagged it with jquery , I would say use .prepend()
:
var table = $("#myTable");
table.prepend('<tr><td><input type="text" name="naamoptie2[0]" class="simple_field" placeholder="Naam van optie/onderdeel" required style="padding: 10px 10px;" /></td></tr>');
Use insertRow(-1)
instaed of insertRow(0)
to append row at last like below.
function myFunction() {
var table = document.getElementById("myTable");
var row = table.insertRow(-1);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
var cell4 = row.insertCell(3);
var cell5 = row.insertCell(4);
cell1.innerHTML = '<input type="text" name="naamoptie2[0]" class="simple_field" placeholder="Naam van optie/onderdeel" required style="padding: 10px 10px;" />';
cell2.innerHTML = '€ <input type="number" name="prijsticket[0]" class="simple_field" placeholder="100" required style="padding-top: 5px; padding-bottom: 5px; min-height: 30px;" step="any" />';
cell3.innerHTML = '<input type="number" name="aantaltickets2[0]" class="simple_field" required style="padding-top: 5px; padding-bottom: 5px; min-height: 30px;" placeholder="aantaltickets" />';
cell4.innerHTML = '<input type="text" name="onderdeelnummer[0]" class="simple_field" required style="padding: 10px 10px; margin-top: 10px;" placeholder="Onderdeelnummer" />';
cell5.innerHTML = '<input type="text" name="categorienummer[0]" class="simple_field" required style="padding: 10px 10px; margin-top: 10px;" placeholder="Categorienummer" />';
}
innerHTML
will replace the existing content with new content.
you have to try prepend()
尝试这个,
$('#myTable tr:last').after('<tr>...</tr><tr>...</tr>');
You could use createElement() and appendChild() instead of insertRow
:
function myFunction() { var table = document.getElementById("myTable"); var row = document.createElement('tr'); //Create row var cell1 = row.insertCell(0); var cell2 = row.insertCell(1); var cell3 = row.insertCell(2); var cell4 = row.insertCell(3); var cell5 = row.insertCell(4); cell1.innerHTML = '<input type="text" name="naamoptie2[0]" class="simple_field" placeholder="Naam van optie/onderdeel" required style="padding: 10px 10px;" />'; cell2.innerHTML = '€ <input type="number" name="prijsticket[0]" class="simple_field" placeholder="100" required style="padding-top: 5px; padding-bottom: 5px; min-height: 30px;" step="any" />'; cell3.innerHTML = '<input type="number" name="aantaltickets2[0]" class="simple_field" required style="padding-top: 5px; padding-bottom: 5px; min-height: 30px;" placeholder="aantaltickets" />'; cell4.innerHTML = '<input type="text" name="onderdeelnummer[0]" class="simple_field" required style="padding: 10px 10px; margin-top: 10px;" placeholder="Onderdeelnummer" />'; cell5.innerHTML = '<input type="text" name="categorienummer[0]" class="simple_field" required style="padding: 10px 10px; margin-top: 10px;" placeholder="Categorienummer" />'; table.appendChild(row); //Append row }
<button onclick="myFunction()">Voeg meer tickets toe</button> <table id='myTable' border='1'></table>
Please try this solution.
<body>
<table id="myTable">
</table>
<button onclick="myFunction()">Voeg meer tickets toe</button>
<script>
function myFunction() {
var table = document.getElementById("myTable");
var len = table.rows.length;
var row = table.insertRow(-1);
row.id = "row_" + len;
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
var cell4 = row.insertCell(3);
var cell5 = row.insertCell(4);
cell1.innerHTML = '<input type="text" name="naamoptie2[0]" class="simple_field" placeholder="Naam van optie/onderdeel" required style="padding: 10px 10px;" />';
cell2.innerHTML = '<input type="number" name="prijsticket[0]" class="simple_field" placeholder="100" required style="padding-top: 5px; padding-bottom: 5px; min-height: 30px;" step="any" />';
cell3.innerHTML = '<input type="number" name="aantaltickets2[0]" class="simple_field" required style="padding-top: 5px; padding-bottom: 5px; min-height: 30px;" placeholder="aantaltickets" />';
cell4.innerHTML = '<input type="text" name="onderdeelnummer[0]" class="simple_field" required style="padding: 10px 10px; margin-top: 10px;" placeholder="Onderdeelnummer" />';
cell5.innerHTML = '<input type="text" name="categorienummer[0]" class="simple_field" required style="padding: 10px 10px; margin-top: 10px;" placeholder="Categorienummer" />';
}
</script>
</body>
try this
var NewR = $("#myTable tr:last").clone();
NewR.appendTo("#myTable");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.