I have a button which will remove the table row using javascript. Worked well for me, but when it is removed/added that space is consumed by other rows.
How can we make that table stable, no consuming if it is not there or if it is added later?
var countDownDate = new Date("Apr 29, 2019 23:56:26").getTime();
var table = document.getElementById("test");
var row1 = table.insertRow(0);
var row2 = table.insertRow(1);
var row3 = table.insertRow(2);
//row.className = 'timer_bg';
var cell1 = row1.insertCell(0);
var cell2 = row2.insertCell(0);
var cell3 = row3.insertCell(0);
cell2.innerHTML = "Text1";
cell3.innerHTML = "Text2";
var x;
function stime() {
// Update the count down every 1 second
x = setInterval(function() {
// Get todays date and time
var now = new Date().getTime();
// Find the distance between now an the count down date
var distance = countDownDate - now;
// Time calculations for days, hours, minutes and seconds
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
cell1.innerHTML = days + "d " + hours + "h " + minutes + "m " + seconds + "s ";
if (distance < 0) {
clearInterval(x);
document.getElementById("timer").innerHTML = "EXPIRED";
}
}, 1000);
}
function hide() {
clearInterval(x);
table.deleteRow(1);
}
.timer_bg {
background-color: red;
color: white;
}
<table id="test" class="table table-bordered table-responsive">
</table>
<br />
<button onclick="hide()">Hide</button>
<button onclick="stime()">Start time</button>
Demonstration:
When Page loads:
r1 (blank)
r2 (text1)
r3 (text2)
When Start Time
clicked:
r1 (time left)
r2 (text1)
r3 (text2)
When Hide
clicked:
r1 (time left)
r2 (blank)
r3 (text2)
UPDATE
I know how to do with id/class
, appreciated this answer, but in my table there are no id/class
in tr/td
How to do that in the table using javascript where no id/class
defined?