I have the following html:
<table border="1" width="200">
<tr id="tr1">
<td id="TD">1</td>
<td id="TD">5</td>
<td id="TD">10</td>
</tr>
<tr id="tr2">
<td id="TD">$1</td>
<td id="TD">$2</td>
<td id="TD">$3</td>
</tr>
</table>
<button onclick="minus()">Back</button>
<button onclick="plus()">Forward</button>
And I have the following array:
var specials = [
{ qty: '1', price: '1'},
{ qty: '5', price: '2'},
{ qty: '10', price: '3'},
{ qty: '20', price: '4'},
{ qty: '30', price: '5'},
{ qty: '40', price: '6'},
{ qty: '50', price: '7'}
];
Onload the html table shows the first three items of the array. I would like to shift through the items by the click of the two buttons. So if i click "forward", the table shows the next item in line.
I had something like this in mind:
var cols = document.getElementById('tr1').getElementsByTagName('td'), colslen = cols.length;
var cols2 = document.getElementById('tr2').getElementsByTagName('td');
q = -1;
function plus() {
i = -1;
while(++i < colslen){
q = q+1;
cols[i].innerHTML = specials[q].qty;
cols2[i].innerHTML = "$"+specials[q].price;
}
}
But it's obviously flawed in that it doesn't step forward by one, but by 3 - thus not showing any overlapping.
I am sure there is a better way to go about this - right?