0

So I have this program that whenever the expiration date is exactly the same date as today the background of the <div> will change it color. But right now, the background color change only when my condition is == and when I try to use>= or <= the background color does not change. Also the background color change even the expiration date is not the same as the date today

Here is my javascript:

<script type="text/javascript">

var $today = date("m/d/Y");
var $expired = $passport_expiration;

function myDIV() {
  if ($today == $expired) {
    document.getElementById("myDiv").style.backgroundColor="#ff4d4d";
  }
}
</script>

Here's my HTML:

<div class="col-md-6" id="myDiv">
    <div class="alert" role="alert"><span class="glyphicon glyphicon-book" aria-hidden="true"></span>&nbsp;&nbsp;Passport <?php echo "<strong><a href=\"view_latest_passport.php?id=$id\"> $passport</a> /&nbsp; Passport Expiration Date:</strong> $passport_expiration"; ?></div>
</div>

How I get the $today and passport_expiration, it is in my query

Thank you in advance

Kode.Error404
  • 573
  • 5
  • 24
aron pascua
  • 101
  • 2
  • 9
  • 1
    Your code looks like php and so do the syntax. –  Jul 16 '16 at 04:06
  • is that one of the reason why i get the wrong output? – aron pascua Jul 16 '16 at 04:07
  • `date("m/d/Y");` This is PHP function, in javascript we use `new Date() `object. –  Jul 16 '16 at 04:08
  • 1
    You are mixing php with javascript. Dates are object and javascript doesn't have object comparison so you have to do something like this. d1.getTime() === d2.getTime(); http://stackoverflow.com/questions/492994/compare-two-dates-with-javascript – Yasin Yaqoobi Jul 16 '16 at 04:08
  • thanks guys. so I must change `date("m/d/Y");` to `new Date()` – aron pascua Jul 16 '16 at 04:11

1 Answers1

0
<div class="col-md-6" id="myDiv">
    <div class="alert" role="alert">
    <span class="glyphicon glyphicon-book" aria-hidden="true"></span>&nbsp;&nbsp;
    Passport <?php echo "<strong><a href=\"view_latest_passport.php?id=$id\"> $passport</a> /&nbsp; 
    Passport Expiration Date:</strong> $passport_expiration"; ?></div>
    <input type="hidden" id="passport_expire_date" name="passport_expire_date" value="<?php echo $passport_expiration; ?>">
     <?php $date = date("m/d/Y "); ?>
     <input type="hidden" id="today_date" name="today_date" value="<?php echo $date; ?>">
</div>

u need to assign that values in input field then only, it will be validated in javascript

<script type="text/javascript">

var today = document.getElementById("today_date");
var expired = document.getElementById("passport_expire_date");

function myDIV() {
  if ($today == $expired) {
    document.getElementById("myDiv").style.backgroundColor="#ff4d4d";
  }
}
</script>
Kalaivani M
  • 1,250
  • 15
  • 29