How do I update the URL on button click?
November 14, 2017
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!