Change background color of bootstrap button after dropdown selection

In my Flask web app I am using bootstrap 4 dropdown buttons where the html for one of the buttons looks like:

      <div class="dropdown">
        <button class="btn btn-secondary dropdown-toggle" style="{{ buttoncolor }}" role="button" id="dropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
          Valdez
        </button>
        <div class="dropdown-menu" aria-labelledby="dropdownMenuLink">
          <a class="dropdown-item" href="{{ url_for('ak.valdez_stormtracker') }}">Storm Tracker</a>
          <a class="dropdown-item" href="{{ url_for('ak.valdez_seasontracker') }}">Season Tracker</a>
        </div>
      </div>

I would like the button’s background color to change when the url of the page matches one of the url’s of the dropdown options.

I have achieved this on regular buttons (no dropdown) as viewed here:
https://www.snowpacktracker.com/btac/

This is done by using custom.js with:

    // Handles the active state transition on bootstrap nav bar
    // get current URL path and assign 'active' class
    var pathname = window.location.pathname;
    console.log(pathname);

    $('.nav > li > a[href="'+pathname+'"').children().addClass('active')

And with some custom css:

.btn-secondary:not(:disabled):not(.disabled).active {
  background-color: var(--color);
}

In this case, the button is wrapped in an <a> tag:

          <li class="nav-item">
            <a href="{{ btnurl }}">
              <button class="btn btn-secondary" style="{{ buttoncolor }}">{{btntxt}}</button>
            </a>
          </li>

I am unable to find a similar solution that will work for the dropdown case. Although recognizing the URL similar to above would be ideal, it would probably also work just to change the background color of the button if any dropdown item is clicked (and retain the color when the new URL is loaded).