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

Viewed 30

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 class="control-label col-sm-4" for="refno">REF nos :</label>
  <div class="col-sm-4">
  <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 class="form-control-static" style="margin-top: -6px;">
        <input type="text" class="form-control" id="refno" name="refno" value="<?php echo $refno; ?>" disabled>
        
    </p>
  </div>
  <div class="col-sm-10"></div>
1 Answers

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 class="control-label col-sm-4" for="refno">REF nos :</label>
<div class="col-sm-4">
  <br/>
  <p class="form-control-static" style="margin-top: -6px;">
    <input type="text" class="form-control" id="refno" name="refno" value="" disabled />
  </p>
</div>
<div class="col-sm-10"></div>

Related