Why does my javascript variable not submit with my HTML form?

I have the following line in my JS file for a variable I have already defined;

document.getElementById("total").value = total;

I then have the following line in my HTML form which I would like to be set with the total value above;

<input type="hidden" id="total" name="total"/>

However, when I submit my form I cannot see this total value being set? I am referencing this JS file in other spots so I don’t believe it’s a referencing issue. Am I setting the variable incorrectly in the form element?

The value is just blank in my $_POST data

TIA