I have a project. I am working to find a container using an only child in JavaScript.
I want to add a class to the container of the req-address.
I want to take req in Javascript using an only child of this element. How to do it?
const search = document.querySelector('.search-form');
const addresses = document.querySelectorAll('.req-address');
search.addEventListener('keyup', function(e) {
addresses.forEach(function(address) {
if (address.innerHTML === search.value) {
address.classList.add('.search-active');
}
});
});
<div class="reqs-container">
<div class="req">
<div class="req-time">
<div class="req-time_from">13:00</div>
<span></span>
<div class="req-time_to">15:00</div>
</div>
<div class="req-restaurant">Argentina Grill</div>
<div class="req-address">Оболонь</div>
<div class="req-name">Аліна</div>
<a href="#" class="req-instagram">Instagram</a>
<a href="#" class="req-confirm">Приєднатися</a>
</div>
<div class="req">
<div class="req-time">
<div class="req-time_from">13:00</div>
<span></span>
<div class="req-time_to">15:00</div>
</div>
<div class="req-restaurant">Argentina Grill</div>
<div class="req-address">Хрещатик</div>
<div class="req-name">Аліна</div>
<a href="#" class="req-instagram">Instagram</a>
<a href="#" class="req-confirm">Приєднатися</a>
</div>
</div>
===? You don't need a strict equality comparison there, the==operator will work just fine.===which to me is an alarming trend (if it wasn't one earlier). I have been writing JavaScript since about 1998 -- long time to learn to see through dubious advice -- and I can count on one hand amount of times I needed===, the code above is certainly not an example where===should be used because a) it surprises the reader b) it isn't necessary (so should be omitted because see point a). I will always point this out and there is no convincing me===has any use beyond where it actually must be used.