Modify the href attribute of all links using javascript only


I have an email template to send on a shipping notification that includes a shipping tracking number as below and I need to clear all the spaces from href using plain javascript:

<a class="press-bt" id="clearSpace" href=" 0008 990 90GB">TRACK</a>

I can get it right using jQuery but not using Javascript

I am trying to do it this way:

window.onload = function() {
    var str = document.getElementById("mylink");
    document.write( str.replaceAll("\\s+","") );

Working code using jQuery:

$(document).ready(function() {
    $('a').attr('href', function (_, val) {
        return val.replace(/\s/g, '');

Target the element by its id, grab the href attribute, and set it again by removing the spaces.

var link = document.getElementById("mylink");
var href = link.getAttribute('href');
link.setAttribute('href', href.replace(/\s/g, ''));