2

I want to change language URL using jquery. My url is https://beta.yourtaxi.ch/de_DE/about-us/ and want to change it just https://beta.yourtaxi.ch/en_Us/about-us/. I have tried below code but not working.

$('a[rel="group"]').on('click', function(e) {
  e.preventDefault();
  location.href = $(this).data('wpurl');
  $(this).attr('href', wpurl);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="sidebar_lang">
  <span>Language:</span>
  <div class="lang_option">
    <a href="<?php echo site_url(); ?>/en_Us/" data-wpurl="<?php echo site_url(); ?>/de_DE/" class="lang_txt" rel="group">DE</a>
    <a href="<?php echo site_url(); ?>/de_DE/" data-wpurl="<?php echo site_url(); ?>/en_Us/" class="lang_txt" rel="group">EN</a>
  </div>
</div>
Albert Einstein
  • 7,472
  • 8
  • 36
  • 71
priyapatel
  • 73
  • 1
  • 1
  • 7

1 Answers1

0

You can get the current path, then remove the first portion to append to the URLs in your <a> tags.

For example

const appendPath = location.pathname // current full path, eg "/de_DE/about-us/"
    .split('/') // split on "/" -> ["", "de_DE", "about-us", ""]
    .slice(2) // omit the first two parts -> ["about-us", ""]
    .join('/') // join in to a slash-delimited string -> "about-us/"

location.href = $(this).data('wpurl') + appendPath
Phil
  • 157,677
  • 23
  • 242
  • 245