Вот пример как в JavaScript, так и в jQuery.
Ванильный JavaScript
function updateCountry (element, country) {
var url = element.getAttribute('href');
if (url.includes('country')) {
url = url.replace(/(\?country=)[A-Z]{2}/, `$1${country}`)
} else {
url = url.concat(`?country=${country}`)
};
element.setAttribute('href', url);
console.log(element.getAttribute('href'));
}
var element = document.getElementById('courses');
updateCountry(element, 'US'); // Add
updateCountry(element, 'UK'); // Update
<a id="courses" href="/courses">Courses</a>
jQuery
function updateCountry (element, country) {
var url = element.attr('href');
if (url.includes('country')) {
url = url.replace(/(\?country=)[A-Z]{2}/, `$1${country}`)
} else {
url = url.concat(`?country=${country}`)
};
element.attr('href', url);
console.log(element.attr('href'));
}
var element = $('#courses');
updateCountry(element, 'US'); // Add
updateCountry(element, 'UK'); // Update
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a id="courses" href="/courses">Courses</a>