How to add/append random number to url hidden field via javascript

  • 21 December 2020
  • 0 replies
  • 92 views

This topic was mentioned in other threads but they were either incomplete or incrorrect.

Here is the right way to add a random number to a hidden field after form is submitted.

  1. Add the following script to

     <script type="text/javascript">
    

    document.addEventListener(“DOMContentLoaded”, function(event) {
    var randomNum = ‘’;
    randomNum += Math.round(Math.random() * (100000 - 10000) + 10000);
    var elem = document.getElementById(“external_id”).value = randomNum;

    var domain = window.location.host + window.location.pathname;
    document.getElementById(“external_id”).value = randomNum;
    });

       </script>
    
  2. Add a hidden field in your form and give it an id name

  3. In the javascript replace the 2 instances that say “external_id” to your hidden field name

The script will generate a number between 10,000 - 100,000


0 replies

Be the first to reply!

Reply