How to use JS to substitute the URL tech. pages in href + add a tail to the link?

For example, we are on page /page
There is code:
<div class="classname"><a href="">EN</a></div>
I want to frame /page in the code + add a "tail" EN
And get something like:
<div class="n=classname"><a href="/pageru">EN</a></div>

Being situated in the page /page, one must click on EN to get on /pageru
How to do it in JS or jQuery?
March 20th 20 at 11:30
1 answer
March 20th 20 at 11:32
document.querySelector('div.classname a').href = window.location.pathname + 'EN';
Hmmm.. works well. I was expecting a longer code )
Thank you very much.
Another question.. I understand the script processes only the first div in the tree, and I have 2 and for some reason first() and last() not working.
I understand these functions from jQuery and your JS code?
How do I choose the first or second (for example) a div? - Abe50 commented on March 20th 20 at 11:35
@Rachael.Man, document.querySelectorAll('div.classname a') will return all such references in div'Oh
if you want to install for all href
const items = document.querySelectorAll('div.classname a');
items.forEach((item) => item.href = window.location.pathname + 'EN');

same thing using jQuery
$('div.classname a').attr('href', window.location.pathname + 'EN');

if you want only first/last links
$('div.classname a').first().attr('href', window.location.pathname + 'EN');
$('div.classname a').last().attr('href', window.location.pathname + 'EN');
- carmelo_Hills commented on March 20th 20 at 11:38
@beulah21, thank you for your detailed answer! - Abe50 commented on March 20th 20 at 11:41
@beulah21 plz tell me how to remove EN from the links?
ie to put the url in the href, but not to add and remove EN - Abe50 commented on March 20th 20 at 11:44
@Rachael.Man, you are lucky that it is now gone))
all similar, but instead of the window.location.pathname + 'EN' write window.location.pathname.slice(0, -2)
if I understand correctly - carmelo_Hills commented on March 20th 20 at 11:47
@beulah21, Yes, I
but not yet test, now try
Thank you ) - Abe50 commented on March 20th 20 at 11:50

Find more questions by tags JavaScriptjQuery