I have a table and a input where a person is supposed to enter a month 1-12, and when submitting, the rows from that month should turn red. How do I get the month out of these dates to determine which month they are from? I am unsure how to continue.
<input type="text" placeholder="Kuukausi" id="searchMonth"/>
<button type="submit" id="searchbutton">
<span>Etsi</span></button>
</div>
<table style="width: 100%" id="table">
<tr>
<th>Lintu</th>
<th>Päivä</th>
</tr>
<tr>
<td>Varis</td>
<td>20.4.2018</td>
</tr>
<tr>
<td>Huuhkaja</td>
<td>14.9.2018</td>
</tr>
<tr>
<td>Pääskynen</td>
<td>24.4.2018</td>
</tr>
<tr>
<td>Peippo</td>
<td>30.3.2018</td>
</tr>
➤Go to Home Tab>>Conditional Formatting Dropdown>>Highlight Cells Rules Groups>>A Date Occurring Option. Then, A Date Occurring Wizard will open. ➤Select Last Month Option from the left-side box Dropdown and Light Red Fill Option from the right-side box Dropdown (or any other option).
type="number"
input elementtbody
rows
and TableRowElement cells
objects"input"
event to trigger a searchMonth()
function 1
th index cell and split its content by .
dots to retrieve the month string. Element.classList.toggle('className', boolean)
to toggle the CSS .highlight
class const tbody = document.querySelector("#tableTbody");
const searchMonth = evt => {
const monthValue = evt.target.value.trim();
[...tbody.rows].forEach(row => {
const month = row.cells[1].textContent.split('.')[1];
row.classList.toggle('highlight', monthValue == month);
})
};
document.querySelector("#searchMonth").addEventListener('input', searchMonth);
.highlight td {
background: yellow;
}
<label>Month: <input type="number" id="searchMonth" min=1 max=12></label>
<table>
<thead>
<tr>
<th>Foo</th>
<th>Date</th>
</tr>
</thead>
<tbody id="tableTbody">
<tr>
<td>a</td>
<td>20.4.2018</td>
</tr>
<tr>
<td>b</td>
<td>14.9.2018</td>
</tr>
<tr>
<td>c</td>
<td>24.4.2018</td>
</tr>
<tr>
<td>d</td>
<td>30.3.2018</td>
</tr>
</tbody>
</table>
Something like this would work:
// handle click and add class
$('#searchbutton').on("click", function() {
$('.highlight').removeClass('highlight');
var rows = $('#table').find('tr');
var value = $('#searchMonth').val().trim();
$.each(rows, function() {
var dateCol = $(this).find('td').eq(1);
if (dateCol.length > 0) {
var date = dateCol.text();
var month = (date.match(/\d{2}.(\d{1,2}).\d{4}/) || [])[1];
if (month == value) {
$(this).addClass('highlight');
}
}
});
})
.highlight {
background: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" placeholder="Kuukausi" id="searchMonth"/>
<button type="submit" id="searchbutton">
<span>Etsi</span></button>
<table style="width: 100%" id="table">
<tr>
<th>Lintu</th>
<th>Päivä</th>
</tr>
<tr>
<td>Varis</td>
<td>20.4.2018</td>
</tr>
<tr>
<td>Huuhkaja</td>
<td>14.9.2018</td>
</tr>
<tr>
<td>Pääskynen</td>
<td>24.4.2018</td>
</tr>
<tr>
<td>Peippo</td>
<td>30.3.2018</td>
</tr>
</table>
Firstly, You can get month value by using split
like currentText.split(".")[1]
Secondly, The input type of searchMonth
should be number
to allow number value only.
$("#searchbutton").on("click", function(){
var searchMonth = $("#searchMonth").val();
$("table tr").each(function(){
var currentText = $(this).find(":eq(1)").text();
var currentMonth = currentText.split(".")[1];
var background_color = searchMonth == parseInt(currentMonth) ? "#00ff78" : "";
$(this).css("background-color", background_color);
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="number" placeholder="Kuukausi" id="searchMonth" value = "4"/>
<button type="submit" id="searchbutton">
<span>Etsi</span></button>
</div>
<table style="width: 100%" id="table">
<tr>
<th>Lintu</th>
<th>Päivä</th>
</tr>
<tr>
<td>Varis</td>
<td>20.4.2018</td>
</tr>
<tr>
<td>Huuhkaja</td>
<td>14.9.2018</td>
</tr>
<tr>
<td>Pääskynen</td>
<td>24.4.2018</td>
</tr>
<tr>
<td>Peippo</td>
<td>30.3.2018</td>
</tr>
<tr>
<td>Peippo</td>
<td>20.02.2018</td>
</tr>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With