Here's what I have:
<a href="<?=$arItem["LINK"]?>"><?=$arItem["TEXT"]?></a>
It's a PHP array, which contains some links. I need to add an extra hash parameter #nav-link
to the end of each link.
Here's how I tried do it:
<a id="likeLink" href=""><?=$arItem["TEXT"]?></a>
<script>
$(document).ready(function () {
$("#likeLink").attr("href", <?=$arItem["LINK"]?> + "#nav-link");
});
</script>
But this code doesn't work because jQuery will not know which links I am linking to. So I guess that I need to generate the unique ids
, but don't know how to do it.
The simplest way to generate identifiers is by a serial number. A steadily increasing number that is assigned to whatever you need to identify next. This is the approached used in most internal databases as well as some commonly encountered public identifiers.
In JavaScript, you can generate a random number with the Math. random() function.
Uuid v4 is a React library or package that creates a universally unique identifier (UUID). It is a 128-bit unique identifier generator. The bits that comprise a UUID v4 are generated randomly and with no inherent logic.
I didn't need a forever unique/random id, just something that was unique per page, and all of these solutions seemed overkill for me so I came up with this:
var uniqId = (function(){
var i=0;
return function() {
return i++;
}
})();
For generating random ids you could use this one.
<script type="text/javascript">
function Generator() {};
Generator.prototype.rand = Math.floor(Math.random() * 26) + Date.now();
Generator.prototype.getId = function() {
return this.rand++;
};
var idGen =new Generator();
</script>
</html>
<body>
<!-- Place this in the body of the page content -->
<button onclick="console.log(idGen.getId())">click</button>
</body>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With