Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок+ 2
a button do thing on clicking & undo it on another click
How can I make a button that when I click on it something appears, and when I click on it again that thing disappears, and this act keeps repeating on clicking. For example, let's say I have a heading (h1), and when I click on it, a topic related to it appears(<div> or <p>), and when I click on the heading again that topic disappears
6 ответов
+ 3
you can do this with if else statements in javascript.
el.on(‘click’, () => {
if (el.style.display === “none”)
el.style.display = “block”;
else
el.style.display = “none”;
});
+ 2
In a div create h1 (heading) and a p (for message).
Now create a new class in css
.classname{
display :none; only
}
On h1 add a click event function
In fxn access p let's var para
Then
Para.classList.toggle("classname") ;
Hope it will work
+ 2
Brian R
Thank you so much♥️
That's so easy, I don't know how I didn't think about it.. I have to fix my brain so that it thinks well😂
+ 2
👍👍
+ 1
Brian R if el is display none how one can click on it to make it display block???
Your click event is on the same element.
does it work??
+ 1
Divya Mohan He just made a mistake, we can change the variable of the other element to another one.. it's about the idea not the details😊