Search code examples
javascriptjqueryuniqueidentifierphpjs

uniqid() in javascript/jquery?


what's the equivalent of this function in javascript:

http://php.net/manual/en/function.uniqid.php

Basically I need to generate a random ID that looks like: a4245f54345 and starts with a alphabetic character (so I can use it as a CSS id)


Solution

  • Try this (Work in php).

    $prefix = chr(rand(97,121));  
    $uniqid =  $prefix.uniqid(); // $uniqid = uniqid($prefix);
    

    Try this for JavaScript::

    var n = Math.floor(Math.random() * 11);
    var k = Math.floor(Math.random() * 1000000);
    var m = String.fromCharCode(n) + k;