'Update Data Attribute via URL Parameter

I'm trying to get this div:

<div id=form data-form-id=z0y9x8 data-user-id=a1b2c3></div>

to modify the data-user-id from the url:

https://example.com/?user=d4e5f6

so it results in this:

<div id=form data-form-id=z0y9x8 data-user-id=d4e5f6></div>

Here's the code I'm using, but things aren't connecting properly.

<script>    
    function getUrlVars() {
    var vars = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
        vars[key] = value;
    });
    return vars;
    }
    var user = getUrlVars()["user"];         
    var form = document.getElementById("form");
    form.setAttribute("data-user-id", user);
</script>

What am I missing?

EDIT: Updated with Barmar & Tim's suggestions. Still no dice though.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source