Untitled UI logotext
Solutions
WebsitesEcommerceMobile AppsWeb AppsProduction Support & Maintenance
Our work
Company
About usBlogPodcastContact us
Book a free consultation

How do I update the URL on button click?

Olivia Rhye

Do you have AJAX on a page and want to change the URL accordingly based on the new content? The following code will get rid of the last bit of the slug on your page and replace it with the data-slug attribute of the button or element you clicked.

Make sure to add an element on the page that has a class of ‘tab‘, and a ‘data-slug‘ attribute from which we will pull and update the slug.

Then set up your jquery click event:

$('.tab').on('click', function(){
    //grab the slug from the element you clicked
     var post_slug = $(this).data("slug");
     history.replaceState(null, null, post_slug + "/");
});

‍

…And there you have it!

Ready to start a project?

Book a free consultation
Untitled UI logotext
Our work
About us
Blog
Careers
Submit a ticket
Agency Partnerships
Contact
© 2024 fjorge. All rights reserved.
Privacy