Click here to Skip to main content
15,867,835 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
This is my source code. The date format script doesn't work at both date input section which is class 'date'. Before this I am used 'id' instead of 'class' but the script only works at first date input section only.

=

  <p><input type="text" name="Idbuku" value="<?php echo $BookId ?>" placeholder="ID Buku" readonly ></p>
  <p><input type="text" name="Judul" value="<?php echo $Bookjudul ?>" placeholder="JUDUL BUKU" readonly></p>
  <p><input type="text" name="Jenis" value="<?php echo $Bookjenis ?>" placeholder="JENIS BUKU" readonly></p>
  <p><input type="text" name="Penulis" value="<?php echo $Bookpenulis ?>" placeholder="PENULIS BUKU" readonly></p>
  <p><input type="text" name="Username" value="<?php echo $var_value ?>" placeholder="NAMA PELAJAR" readonly></p>

  <p><input type="date" name="Tpinjam" value="" placeholder="TARIKH PEMINJAMAN" class="date" required></p>
  <p><input type="date" name="Tpulang" value="" placeholder="TARIKH PEMULANGAN" class="date" required></p>
 <script>
      let DateToday=new Date();
      let month=DateToday.getMonth()+1;
      let day=DateToday.getDate();
      let year=DateToday.getFullYear();
      if(month<10)
      month='0'+month.toString();
      if(day<10)
      day='0'+day.toString();
      let Today=year+'-'+month+'-'+day;
      let maxdate=year+1+'-'+month+'-'+day;
      document.getElementByClassName('date')[0].setAttribute("min",Today);
</script>


  <p class="submit"><input type="submit" name="Login" value="DAFTAR"></p>
  </form>


What I have tried:

I have tried to use 'id' and set diffrent 'id' for both date input section but the script only work at first input section and doesn't work at second date input.
Posted
Updated 31-Dec-21 17:06pm

1 solution

document.getElementsByClassName(name) returns a HTMLCollection

Try to iterate through each element within that collection using a for loop:

JavaScript
var elements = document.getElementsByClassName("date");

for (var i=0; i < elements.length; i++)
{
  elements[i].setAttribute("min", Today);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900