Reputation: 3982
I have a page which has an anchor element:
<a id="courses" href="/courses">Courses</a>
And I want to add or update some params to the href
, for example:
Add a country
params: <a id="courses" href="/courses?country=US">Courses</a>
Update the country
params from US
to UK
: <a id="courses" href="/courses?country=UK">Courses</a>
What's the best way to do it via JavaScript or jQuery?
Upvotes: 2
Views: 2089
Reputation: 68933
You can pass a function as the second parameter to the .attr()
to modify the href attribute.
Try the following way:
// set
$("#courses").attr('href','/courses?country=US');
console.log($("#courses").attr('href'));
// update
$('#setParam').click(function(){
$("#courses").attr('href', function(_, el){
return el.replace(/(country=)[a-z]+/ig, '$1UK');
});
console.log($("#courses").attr('href'));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a id="courses" href="/courses">Courses</a>
<button id="setParam" type="button">Set Param</button>
Upvotes: 2
Reputation: 3604
Here's an example in both JavaScript, and jQuery.
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>
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>
Upvotes: 0
Reputation: 2168
Wrrite a custom function to handle adding properties to the href attribute.
// function to change href attribute
function addPropertyToCourses(prop,value) {
var courses = document.getElementById('courses');
if (courses.href.indexOf("?") == -1) {
courses.href += "?";
} else {
courses.href += "&";
}
courses.href += prop + '=' + value;
}
(function() {
// add properties
addPropertyToCourses("country", "UK");
addPropertyToCourses("someProp", "someValue");
})();
<a id="courses" href="/courses">Courses</a>
Upvotes: 1
Reputation: 21499
Use jquery .attr( function )
to add/edit href
attribute of element. In function check if attribute hasn't country
parameter, add to it and if has edit it.
$("#courses").attr("href", function(i, href){
var index = href.indexOf("?");
return (index == -1 ? href : href.substring(0, index)) + "?country=US";
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a id="courses" href="/courses">Courses</a>
Upvotes: 0