12Hannibal12 12Hannibal12 - 2 months ago 11
Javascript Question

Get date from <td> if there is one

There are multiple table rows. The table cell with class "dates" can contain a date or the value "Permanent". If there is a date and this is greater than today's date, it should be colored in red.

I tried (but won't work):

if(($(".dates").getDate() < new Date()) != -1) {
$(".dates").style.color = "green";
}


HTML:

<table>
<tr>
<td>Permanent</td>
</tr>
<tr>
<td>Permanent</td>
</tr>
<tr>
<td>2.05.2015</td>
</tr>
</table>

Answer

$(".dates").each(function(){
  
  if(new Date($(this).html()) < new Date())  {
        $(this).css("color", "green");
  }
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
		<tr>
			<td>abc</td>
			<td class="dates">2016-10-10</td>
			<td>abc</td>
		</tr>
		<tr>
			<td>abc</td>
			<td class="dates">2016-09-10</td>
			<td>abc</td>
		</tr>
		<tr>
			<td>abc</td>
			<td class="dates">2016-08-10</td>
			<td>abc</td>
		</tr>
		<tr>
			<td>abc</td>
			<td class="dates">2016-11-10</td>
			<td>abc</td>
		</tr>
</table>