Home > Enterprise >  How to show today's year and random 4 digit numbers at the same time?
How to show today's year and random 4 digit numbers at the same time?

Time:09-06

Currently, I have 4 digit numbers showing randomly if I refresh the page. I tryna add the years infront of the 4 digit numbers but only 4 digit numbers still showing. Is there anyway to show the years in front of random numbers? (Example, 20221234).

<label  for="refno">REF nos :</label>
  <div >
  <script type="text/javascript">
    now = new Date();
    randomNum = '';
    randomNum  = Math.round(Math.random() * 9);
    randomNum  = Math.round(Math.random() * 9);
    randomNum  = now.getTime().toString().slice(-2);
    window.onload = function () {
        document.getElementById("refno").value = randomNum;
    }
    document.getElementById("refno").innerHTML = new Date().getFullYear();
</script>
    <p  style="margin-top: -6px;">
        <input type="text"  id="refno" name="refno" value="<?php echo $refno; ?>" disabled>
        
    </p>
  </div>
  <div ></div>

CodePudding user response:

You can't use innerHTML on an input tag.
You can use document.getElementById('someID').value to replace the value.

const now = new Date();
let randomNum = '';
randomNum  = Math.round(Math.random() * 9);
randomNum  = Math.round(Math.random() * 9);
randomNum  = now.getTime().toString().slice(-2);
document.getElementById("refno").value = `${new Date().getFullYear()}${randomNum}`;
<label  for="refno">REF nos :</label>
<div >
  <br/>
  <p  style="margin-top: -6px;">
    <input type="text"  id="refno" name="refno" value="" disabled />
  </p>
</div>
<div ></div>

  • Related