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?
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=)[AZ]{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=)[AZ]{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>
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>
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>
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=)[az]+/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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.