How to generate 3 unique random numbers and use it as an ID in drupal webform?

1k views Asked by At

I have a custom js file in my custom module, which generates a 3-digit ID,

function genDigit() {
   return Math.floor(Math.random()*899+100);
}

I would like to add this ID in my webform as follows:

  1. When a user opens a webform page in the browser, the js is called from the custom module (assuming the module is active)
  2. The generated ID is passed automatically to the webform in a hidden field
  3. When submitting the form, the ID is saved as well alongside the other data.
1

There are 1 answers

0
daggakilla On

I would suggest something like this:

drupal_add_js('jQuery(document).ready(function(){jQuery("#ID_ELEMENT").val(Math.floor(Math.random()*899+100));});','inline');

Whereby you put the js in the drupal_add_js function itself, instead of calling an external js file to do so. By the way this function does not generate a unique ID, but can give you an idea how to proceed from here.

But the idea of adding such ID to a webform is indeed curious, as any webform has a unique ID already as @CornelAndreev mentioned earlier. Good luck.