Simple JS action?

Hello, please tell me is it possible to make js or jquery, same thing.
If the address page displays the name of the links that we have this link assigned to the class ?
---
In other words, if the site address is the address of the page to index1 or index2, then the link which contains this page we assign the class ?
Is:
<a href="https://site-name/index1">Name 1</a>
<a href="https://site-name/index2">Name 2</a>
<a href="https://site-name/index3">Name 3</a>
April 7th 20 at 15:41
5 answers
April 7th 20 at 15:43
Solution
document.querySelectorAll('a').forEach(link => {
 if (link.pathname === window.location.pathname) {
link.classList.add('active')
 } else {
link.classList.remove('active')
}
})
April 7th 20 at 15:45
the <script>
$(document).ready(function(){

if(window.location.pathname == '/index.php/pages') {
$('a.custom-class').addClass("add-custom-class");
}
})
</script>
April 7th 20 at 15:47
Easy! Use the class to work with URLS.
To current address:
let url = new URL(document.location.href)
do the analysis of the url, I advise you to look at
console(document.location);
console(document.location.pathname);

there you will find parse your URL and will be able to pull the required option or link for analysis.

Then form a link with class depending on the conditions.
April 7th 20 at 15:49
`window.location.pathname` - then your reference settings
$('a[href$=window.location.pathname]').addClass('myclass');
like this.
April 7th 20 at 15:51
Can.
Get the href attribute, trim/regex/count the number of characters from the end/beginning. To check condition. Add class.

Find more questions by tags JavaScriptjQuery