I'd like to know how to find a particular <tr>
element in a numbered table. For example: In a table where each <tr>
line has its <td>
cells numbered 1-5, containing 4 lines <tr>
, if I pass the number 15, I get the 3rd line <tr>
, if I pass the number 17, I get the 4th line <tr>
, and so on.
With table.match(/<tr>/g)
, I get all 4 elements <tr>
, but what I want is to get only a <tr>
element, as final result, according to the numerical criteria explained above.
The goal is to apply a style to the <tr>
tag, an outline, outline: solid 1px red;
, to highlight.
var table = ['<table>
<tr>
<td>1</td><td>2</td><td>3</td><td>4</td><td>5</td>
</tr>
<tr>
<td>6</td><td>7</td><td>8</td><td>9</td><td>10</td>
</tr>
<tr>
<td>11</td><td>12</td><td>13</td><td>14</td><td>15</td>
</tr>
<tr>
<td>16</td><td>17</td><td>18</td><td>19</td><td>20</td>
</tr>
</table>'];
table.match(/<tr>/g); // Me devolve todos os elementos <tr>
The table is originally the function below that generates a Calendar. I've already been able to highlight the current day . Now the challenge is to do the same with the current week :
function Calendar(id, year, month) {
var elem = document.getElementById(id)
var mon = month - 1
var d = new Date(year, mon)
var e = new Date();
var weekDay = ["Sun", "Mon", "Tues", "Wed", "Thu", "Fri", "Sat"];
var months = ['January', 'Frebruary', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
var table = ['<table><tr>']
table.push('<th colspan = 4>' + months[mon] + '</th>' + '<th colspan = 3>' + year + '</th>' + '</tr><tr>')
for (var i = 0; i <= 6; i++ ) {
table.push('<th>' + weekDay[i] + '</th>')
}
table.push('</tr><tr>')
for (var i=0; i<d.getDay(); i++) {
table.push('<td></td>')
}
while(d.getMonth() == mon) {
if (d.getDate() == e.getDate()) { // Condicional para dar destaque ao dia atual
table.push('<td class=day>'+d.getDate()+'</td>')
} else {
table.push('<td>'+d.getDate()+'</td>')
}
if (d.getDay() % 7 == 6) {
table.push('</tr><tr>')
}
d.setDate(d.getDate()+1)
}
for (var i=d.getDay(); i<7; i++) {
table.push('<td></td>')
}
table.push('</tr></table>')
elem.innerHTML = table.join('\n')
}
new Calendar("cal", 2015, 3);