A
A
Aaang2020-05-12 10:39:43
JavaScript
Aaang, 2020-05-12 10:39:43

How to compare url and highlight link in js?

The task is such that when a user enters a page from one of the sites, there should be a check, and if the url of the site finds a link in the list of cities and with the desired href, then style is added. Where is the mistake? I so understand that I cannot take .href through querySelectorAll.

const someHrefLink = document.querySelectorAll(".some-href-link").href;

if(document.location.href === someHrefLink.forEach(el => el.style.backgroundColor = 'red');


How to implement this code correctly?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex, 2020-05-12
@Kozack

I so understand that I cannot take .href through querySelectorAll.

Because querySelectorAll returns not an element, but a NodeList.
You need something like this:
[...document.querySelectorAll(".some-href-link")].map(a => a.href) // Массив всех href

And to solve the problem, you need to check href in a loop, going through the NodeList of all links. And change styles there.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question