Answer the question
In order to leave comments, you need to log in
How to change the class of an element when clicking on another element using js?
Hello.
The bottom line is that I need to change the class in a certain block, but I have it changing in all blocks.
<div class="main">
<div class="but">кнопка</div>
<p class="text">изменить текст</p>
</div>
<div class="main">
<div class="but">кнопка</div>
<p class="text">изменить текст</p>
</div>
<div class="main">
<div class="but">кнопка</div>
<p class="text">изменить текст</p>
</div>
<div class="main">
<div class="but">кнопка</div>
<p class="text">изменить текст</p>
</div>
Answer the question
In order to leave comments, you need to log in
$id = $_SESSION['logged_user']->id;
$user = R::load('users', $id);
$user->vk = $data['vk'];
R::store($user);
let mains = [ ...document.querySelectorAll(".main") ];
mains.forEach(main => {
let btn = main.querySelector(".but"),
text = main.querySelector(".text");
btn.addEventListener("click", () => text.classList.toggle("some-class"));
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question